- Home /
Alternatives to Instantiate/Destroy
Problem: Create an unlimited track.
I have two prefabs track1 and track2. Currently I destroy the current object and create a new one by adding some offset and translating them.
On the mobile there is no way I can create the entire track as once loaded it will slow down the system considerably. Was wondering how taxing this mechanism of Instantiate/destroy is?
Is there a better way of doing this for instance disable the same object, translate it and then enable it again? if yes how?
Any help would be appreciated.
Answer by fafase · Jun 17, 2012 at 09:22 AM
You should consider replacing the track instead of destroying it.
When calling Destroy(), you call the Garbage Collector and with it all kind of internal functions for memory management.
Instead of destroying the object you could reposition it where you need it when it is off camera. Considering you are using a phone, that would save quite a lot of cycles.
Just place your two track in a built-in Array of Game Object and use their index for position.
Simply arrayName[i].transform.position = Vector3(x,y,z); Use the positions you were using in the Instantiations. You might want to cache that to go even faster.
var pos :Vector3[2];
function Start(){
pos[0] = arrayName[0].transform.position;
pos[1] = arrayName[1].transform.position; }
function Update(){
pos[0] = Vector3(x,y,z);
pos[1] =Vector3(x,y,z); }