So… Let’s say I make a game where a player kill another player. After that the dead player will drop blood or something like that (instantiatin a lot of sprite) and it perhaps cause frame drops. My question is if my blood sprite are already in game (unactive) off scree. If I call them when I need it will cause less frame drops.
I haven’t a game like this yet but I want to know which way is better. Maybe and why
Avoid using Instantiate if you can, I’ve noticed that it’s quite an expensive function.
I will try, thx
It can be an optimization if you need to retreive new instances of a type frequently, but you shouldn’t do it with every object. Sometimes you’re better off doing it the normal way.
That being said, the profiler will definitely be able to tell you whether it’s a useful optimization in a particular situation.
This does not only apply to games btw, but to programming in general.
It is sometimes also referred to as (object/instance) pooling.
I don’t know what pooling is. I find some tutorial and I will learn as soon as possible
Object pooling means you create a number of objects (a pool of objects) that you reuse as needed by setting them active/inactive instead of creating and destroying objects. If you constantly create and destroy objects it will generate garbage in memory and cause garbage collection to trigger more frequently which can slow down your game.
Here is a video tutorial on object pooling:
Thx so much for this link and for the explanation of what object pooling is
You guys don’t know if it’s actually a CPU problem. Instantiate is a temporary CPU hit. It doesn’t really cost much if he’s doing it a few times per frame.
What is instantiated is the real cost, and if it’s just an effect sprite like this, it is likely the cost is actually in draw calls or on GPU.
So the correct answer you guys should’ve given is “profile it first”.
:)) A very simple answer but usefull