Question by
Raidenwins · Feb 18, 2018 at 04:38 PM ·
prefabprefab-instance
After being instantiated once and then destroyed, prefab is null
I am trying to accomplish the following: instantiate a prefab, destroy it, and then instantiate it again, destroy it again, and so on, and so forth. Let's call the prefab MyPrefab. MyPrefab has a MonoBehaviour script attached to it called MyPrefabMonoBehaviour.
In another MonoBehaviour script, called PrefabScript, I instantiate the prefab like so:
public class PrefabScript : MonoBehaviour
{
public GameObject MyPrefab;
public GameObject CreatePrefab(Vector3 position, Quaternion rotation)
{
GameObject prefabInstance = (GameObject) Instantiate(MyPrefab, position, rotation);
return prefabInstance ;
}
}
And then, in the MonoBehaviour attached to MyPrefab, I destroy it:
public class MyPrefabMonoBehaviour : MonoBehaviour
{
private void DoSomething()
{
// Do something else
Destroy(gameObject);
}
}
This works fine the first time I instantiate the prefab, but when I try to do it a second time the value of MyPrefab in PrefabScript.CreatePrefab is null.
I have also tried destroying the prefab from a third MonoBehaviour, which is where I am calling PrefabScript.CreatePrefab from, but the result was the same.
What is the problem here?
Comment