Put a prefab in Hierarchy without using Instantiate ? (from code)


I was wondering if it was possible to put a prefab (as a game object and from code) in the scene without using Instantiate. I mean doing like a drag & drop but from code, without instantiating the prefab in c#. I know that when you put a prefab in the hierarchy by draging it with the mouse, it’s not an instance… I want to do the same but in C# (script). I tried to use new GameObject and set the prefab as a child but it causes an error (data corruption). Any idea ?


you cant put the prefab in the scene, it will always be an instance of it.

the data corruption error mean that you are trying to modify the prefab and unity wont let you do that.

Also why would you want to avoid instancing a prefab? This is only a reference to the file in your assets, it virtually cost nothing

And finally, when you drag and drop a prefab in your hierarchy, it is an instance of the prefab.

ofc its an instance when you put something in the hierarchy. EVERYTHING in the hierarchy is ( and must be) an instance.

Little detail though, instantiate does cost a lot of cpu usage if you instantiate tons of object (like bullets).
Then it becomes more effiecent to use a objectPooler.
A great leason about objectPooling.