Search Issue Tracker
By Design
Votes
0
Found in
2019.4
2021.1
Issue ID
1284413
Regression
No
Prefab GameObject and Asset lose script when exiting Play Mode
Reproduction steps:
1. Open attached project and scene "Factory"
2. In Hierarchy window, create new GameObject and select it
3. In Inspector window, add a "Dynamic Add Group Test" component
4. For 'Object' variable reference, choose any of the available selections (or find TreeConfig1 in Project window)
5. Enter and Exit Play Mode
6. In Hierarchy window, select "ObjectsRenderer-Instance" GameObject
7. Observe inspector window
8. If the "Objects Renderer (Script)" component is not removed, repeat steps 5-8
Expected result: The prefab GameObject remains as it was
Actual result: The prefab GameObject's "Objects Renderer (Script)" component has been removed
Reproducible with: 2019.4.13f1, 2020.1.10f1, 2020.2.0b8, 2021.1.0b10, 2021.2.0a7
Could not test with: 2018.4.28f1 (project crashes on startup)
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Standalone Player crashes with "TDerived GetOrLoad<TDerived>() where TDerived : T" when IL2CPP Code generation is set to "Faster (smaller) Builds"
- IndexOutOfRangeException and InvalidOperationException when logging XML string
- Script missing in "Assets/Settings/Mobile_Renderer/GlobalVolumeFeature" of "com.unity.template.urp-blank" template
- “Font Asset Creator - Error Code [Invalid_File_Structure]…“ error is logged when generating Font Assets from fonts with meta files from previous Editor versions
- Input.mousePosition returns (NaN, NaN, 0.00) when Scene view is opened
Resolution Note:
This is a problem in ObjectsRenderer which is a Singleton, but the implementation does not take into consideration that the object can be a part of a prefab asset. Furthermore, ObjectsRenderer has [ExecuteInEditMode] which means the prefab asset will also get the lifetime callbacks. When ObjectsRenderer is both on a prefab asset and a prefab instance it actually depends on which one gets called first and that one will be registered as the instance, the other one will be destroyed
private void OnEnable()
{
if (Exists && Instance != this)
{
if (Application.isPlaying) Destroy(this);
else DestroyImmediate(this);
return;
}
ObjectRendererBehaviour.Validated += OnRendererChanged;
CameraCommandBufferManager.Instance.Register(this);
Camera.onPreCull += OnPreCullCamera;
requireRecalculateDrawCalls = true;
}
I suggest fixing this by making sure only non-persistent objects execute this code, thus prevent the prefab assets from registering themselves and making sure multiple instances of the prefab will behave correctly.