- Home /
Instantation Lag In Unity With Simple Game Object
I have a game object composed of 3 planes (with textures) a transparent cube, an empty with a box collider and rigid body, each enemy can instantiate 4 of these about every 50 tics.
Here is the code:
audio.Play();
for( var i = 0; i < guns.length; ++i ) {
var shot = Instantiate( shotType,
guns[ i ].transform.position, guns[ i ].transform.rotation );
}
When I get about 10 enemies this lags a lot, can anyone tell me how I could possibly optimize this or what I can do about such a simple script so it does not lag?
Answer by Clet_ · Apr 29, 2014 at 11:54 PM
You should be using a pool. A pool is a class that generates spare objects that can be used whenever they are needed, but instead of destroying them when they're not needed anymore, you just deactivate them and add them back to the pool for further use.
That way, you avoid instantiating/destroying at runtime, which remains very expensive operations, even on basic objects.
This is not as complicated to implement as it sounds. Just look at this answer posted on another answer
Your answer

Follow this Question
Related Questions
Setting an objects instantation rotation? 2 Answers
Bullet instantiates sometimes in wrong posiiton 0 Answers
Instantiate New Gun 1 Answer
How can i stop my game from laging during instantiation? C# 2 Answers
Projectile Script 1 Answer