Lag, spawn problem

Hi,
I’m doing a game, where object is falling in tube. I have a problem with spawning parts of tube. I wanted to do it in real time but there are lags… My question, is this caused of tube- part is too haevy? Is there any other way to spawn it smooth in real time?

Script:
var respawnPrefab : GameObject;
var other : Transform;

function Start () {

}
var ilosc : int = 0;

function Update(){

ilosc++;
print(“ilosc=” +ilosc);
Instantiate (respawnPrefab, Vector3(0, ilosc * -49.55 , 0), transform.rotation);

}
Tube part:

you need [ Code] [/ Code] tags (without the spaces) around that code matey… if you’re constantly reusing the same objects by instantiating and destroying them you probably need to look up “Object Pooling”

Ok, but will it works better, without lags?

Generally yes.

Instantiation entails a fairly high overhead as it needs to do a few things (behind the scenes) like memory allocation which you have no direct control over. So your script must wait until the Instantiation is finished which is where your lag is coming from.

An object pool will allow you to reuse a game object so you only have to suffer the instantiation performance when you instantiate a new object that isn’t in the pool. Once it’s in the pool you can pseudo instantiate it (using what ever method your object pool provides) which will return an object that is already instantiated and you don’t suffer instantiation lag.

Thanks for help!!