Hi, I have been searching around and would like advise on this if anyone could?
My first question: If you had say 100 Prefabs that you would like to Show/Hide with menu commands, which would be best performance wise.
Using Prefab Instantiate and Destroy?
Or Turning the Object Renderer Enabled on True / False?
** Also the below code, I don’t know why, but destroy dose not work correctly.
Prefab Instantiate and Destroy
// Set in the inspector
var prefab1 : GameObject;
var prefab2 : GameObject;
var pos = Vector3 (0, 0, 0);
function Update ()
{
// Use the first prefab when pressing 1 and the second prefab when pressing 2..
if (Input.GetKeyDown ("1")) {
Instantiate(prefab1, pos, Quaternion.identity);
Destroy (prefab2);
}
if (Input.GetKeyDown ("2")) {
Instantiate(prefab2, pos, Quaternion.identity);
Destroy (prefab1);
}
}
Found this similar question in the iPhone Scripting. Maybe Hide/Show might be better for 100 objects rather than Destroy…
http://forum.unity3d.com/viewtopic.php?t=19915&highlight=show+hide+prefab
When you call Instantiate(prefab1) you’re creating a new object that’s a copy of that prefab. As such, when you destroy prefab1, you’re attempting to destroy the prefab, not the instantiated object.
You should have a global variable like curPrefab1 that you use like this:
curPrefab1 = Instantiate(prefab1, pos, Quaternion.identity);
Destroy(curPrefab1.gameObject); (You can remove .gameObject if it’s of type GameObject)
That way you keep track of the created object and destroy it as opposed to the prefab.
For total system resources used, I’d recommend hiding and showing vs. Instantiating and Destroying. It gives you way fewer problems on platforms like iPhone.
Thanks! Allot! Yeah I think I will go the hiding and showing route. I was abit confused on how each would effect the memory… but this helps allot!