- Home /
Using an instantiated pooled object for the first time causes frame drop
I have a strange situation where when I show a game object on screen for the first time, like a small explosion, particle, object with attached lens flare, etc the frame rate drops for a split second causing touch control response issues.
If it were a card game, it wouldn't matter much but -- for an action, response sensitive game, it's a total kill joy cause the player loses a life for no reason and that's no fun.
Here's the kicker, it only happens the first time that object is put into play. Because after that, when that object is activated again from the pool, there is no stall at all.
These are all small, pre-instantiated, pooled object -- mostly simple particles or 2d explosions which only uses quads so it's not like I'm suddenly adding thousands is tris to the rendering engine.
My draw calls is also very low. On average it the game coasts along with 9 or 10 draw calls per frame. So when I make alive a pooled particle system or 2d explosion with attached lens flare -- I'm only adding, at most, 1 to 3 draw calls for a whopping total of ~13 draw calls. I did not think that was too much to handle.
I've double check to ensure that I am indeed using pooled objects and that the object scripts has no heavy duty code that does any major resource loading or doing any complex computations. By all measures, these are very light weight objects.
Again, it only happens the first time I use one of those pooled objects. All other subsequent uses has no issues at all.
This happens both in the Unity Editor play mode as well as in my final build running on iPad.
I tried using the Profiler on the trial license but could not find anything and now that trial has expired.
What's going on?
Manny
Do you have anything going on in the Start method of any of your scripts? Because the Start method is only called the first frame the script is enabled. Therefore, if you instantiate it in a pool disabled and then enable it at a later time, the Start gets called when you enable them. If this is the case, maybe using Awake helps, since this is called even when disabled. Which means the hickup will happen at initializing the pool as intended.
Hi, this may be because Unity loads some resources (from disk) when you instantiate the first object. Some pooling system include a "preload" option to remove this effect.
There should be some free pooling system you can check or search for "pooling" and "preload".
I am not using any pooling plugins. I wrote my own by simply by Instantiating a gameObject and pushing it onto a stack on level load. That is it.
There is code on my object's Start() method but I allow it to run when instantiating and at the end, I do a SetActive(false). I've confirmed, via Debug.Log, that Start() is not executing when I use a pooled object.
I'm not able to find anything about pooled object "pre-loading" -- can you reference a blog or article for me?
Thanks, $$anonymous$$anny
A thought: When I instantiate by pooled objects, I usually but it off screen at (-1000, -1000, -1000).
The material texture used by my pooled objects, is it loaded into GPU memory upon instantiating or only when it first appears on the screen?
$$anonymous$$anny
That's a good thought. Unity may be doing some unwanted optimization for you since it's not visible. I don't have any documentation to point to, but it's definitely worth googling for.
Answer by manny003 · Nov 06, 2014 at 08:45 PM
Well, well, well, what do ya know ...
Thanks to dns for pointing me in the right direction.