Search Issue Tracker

By Design



Found in


Issue ID




If prefab is instantiated with PrefabUtility.InstantiatePrefab children is not found using GetComponentsInChildren<Transform>

Scene Management


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.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.