Reputation:
I've 2 scripts. 1 makes objects and the other destroys them.
I first press OnSave
button which runs script #1 and creates my objects. (there's more than one, although here in the script just shows one, I shortened for the sake of question)
then I press OnReset
button which destroys created objects/prefabs. But when I press OnSave
again, it doesn't recreated objects.
These 2 scripts are attached to 2 different gameObjects
here's what I have:
script #1:
public GameObject go;
public static volatile List<GameObject> prefab = new List<GameObject>();
public class OnSave : MonoBehaviour
{
public void Start2()
{
var newObj = Instantiate(go);
newObj.transform.parent = GameObject.Find("MOOSE").transform;
newObj.GetComponent<Renderer>().material.color = Color.green;
newObj.transform.localPosition = new Vector3(1.0,1.0,1.0)
prefab.Add(newObj);
}
}
script #2
public class OnReset: MonoBehaviour
{
foreach (GameObject obj in prefab)
{
Destroy(obj);
}
}
I'm using the latest version of Unity. I have to destroy these objects and recreate them
Upvotes: 1
Views: 1097
Reputation: 756
You need a prefab to be able to reinstantiate the object, but the operation of destroying and reinstantiating is heavy, so normally you use a pooling manager of sort like https://www.assetstore.unity3d.com/en/#!/content/47242 (disclaimer: I'm the author of this one) or https://www.assetstore.unity3d.com/en/#!/content/23931.
Here's the official Unity totorial about pooling objects: https://unity3d.com/learn/tutorials/topics/scripting/object-pooling
Upvotes: 4