Search Issue Tracker
By Design
Votes
0
Found in
5.3.0f4
Issue ID
756919
Regression
No
If prefab is instantiated with PrefabUtility.InstantiatePrefab children is not found using GetComponentsInChildren<Transform>
How to reproduce:
1. Open 'Test Scene' in the attached project
2. Delete 'child' object
3. Press play in editor. (child object is instantiated at runtime because it is missing.)
4. Press stop in editor. (child object is instantiated at edit time using prefab instantiation, again because it is missing).
5. Press play in editor. (somehow a new child object appears at runtime, with 'child 1' name, even tho it should not, because a child object was present).
6. Press stop in editor. (the 'child 1' object appears as a prefab this time, because at edit time the code instantiates an object using prefab instantiation, but this should not happen because the object 'child' was present in children of 'parent')
Workaround: If Object.Instantiate is used instead of PrefabUtility.InstantiatePrefab
(in the AddChild method), this behaviour of 'child 1' appearing is not
happening. As then GetComponentsInChildren<Transform> finds children
BY DESIGN: The reason the extra objects appears is because the altered named of the first added GameObject is not recorded using PrefabUtility.RecordPrefabInstancePropertyModifications() (in this case you would pass the gameObject as the argument), so Unity registers it's name as "child 1" rather than "child". Since the script is checking for "child" it will never find any gameObjects, and therefore it keeps instantiating them.
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Caret moves by a character when typing "." and any number into 'Grid and Snap' toolbar's input field
- Missing #pragma directives warnings appear in Inspector when including .hlsl file in Shader
- Fixed Timestep setting gets resets to default when upgrading project
- Console displays "NullReferenceException: Object reference not set to an instance of an object." during Editor Theme change
- Editor interface has low contrast when any theme is used
Add comment