- Home /
Instantiating a gameobject causes a lagspike
I am developing a 2.5D mobile game and i seem to have a problem when the game instantiates the boss after X points. The boss is obviously a heavier gameobject than the rest of the enemies as it has a bigger script, more colliders and more audio clips with it. The mob runs fine but the problem is the instantiation itself as it causes a split-second lagspike when it does it. The game instantiates many mobs every second without a problem so its infact the boss that causes it, weirdly even on an empty scene where other enemy spawners are deactivated... There are also no particles upon spawning, nor animation so I really don't know what it could be. Thanks in advance!
Answer by spidermancy612 · Jan 10, 2019 at 05:29 PM
There's a lot of things it could be, however a better solution might be to simply spawn the object at the start of the scene and set it to inactive. Whenever you're ready to use it, move the object to the spawn position and then set it to active. The "lag spike" will then just be part of the game loading the scene instead of interrupting gameplay, and you can keep the boss the same as you currently desire.
If you're on mobile it might be worthwhile to look into doing this for all your instanciated objects to save on CPU usage. It's a strategy called Object Pooling if you'd like to learn more about it.
Answer by L42yB · Jan 10, 2019 at 05:42 PM
Use an Object Pool and cache your game objects rather than instantiating them at runtime.
https://unity3d.com/learn/tutorials/topics/scripting/object-pooling
Your answer
Follow this Question
Related Questions
Best way to display scores appearance 1 Answer
Lag when activating GameObjects 5 Answers
Do Asynchronous GameObject.Instantiate Exists? 2 Answers
How to be notified of new GamObject creation? 1 Answer
Unwanted 2nd empty gameobject 1 Answer