- Home /
Instantiate a prefab vs create one dynamically at runtime?
I am wondering if there is a performance difference between loading a prefab into the game vs creating a gameobject programatically. I guess I can do either way, but was wondering if one is faster than the other, or if one takes less memory.
Let's say I have 200 objects as prefabs and I only want to select 5 of them, vs. figuring out which 5 I want and then just creating them through code. Is there a huge memory difference between having those 195 prefabs not being used?
Like I said I can do it either way, and I don't think there would be a performance hit but just wanted to see if the community knew any different.
The only thing I could think of is that if the prefabs are big in size that would be the only disadvantage, but I think they would be small so we can rule that out as a factor.
Any insight would be great!
Thanks, Will
There's a small performance cost associated with instantiation, but this is often hidden behind a load screen, or otherwise parceled out over time to hide the cost. Also look into object pooling if you need the same prefabs many times (e.g. projectiles).
It's not usually a question of performance, but of sense. It is either more convenient and sensible to pre-place objects in the scene or to create them on the fly. Do what makes the most sense.
WRT having hundreds of unused objects, it would be wise to avoid that situation if possible, which leads me to assume instantiating would be better in your scenario.
That helps. Thanks :) After some thinking last night I think I have to go with dynamically creating them because of how different the object can be.
Thanks again!
Answer by Ericool · Mar 24, 2015 at 04:26 PM
when you are playing with the editor, it loads your public prefabs or scripts so there is no worry with that , but a good practice is to load them , even other resources and when needed you copy them . That way you wont have any surprise if the prefab was too huge. For the build , big prefabs should not be at runtime, my opinion , most of the time the prefab are always the same and are always instantiate at runtime .Just reminder that for a small game it is ok , but for a huge one , you might want to keep that memory for something else instead .