- Home /
"Destroy assets is not permitted to avoid data loss"
In my script, I have an empty, public array of GameObjects that I fill in the Unity editor. I am able to instantiate them just fine with Instantiate(...), but when I try to destroy them, I get the error: "Destroying assets is not permitted to avoid data loss". Thanks for the help!
Here is a simplified version of what I am trying to do:
//this array is filled in the editor:
public GameObject[] gameObjectArray = new GameObject[0];
//code for instantiating the object (this works fine):
//Note: 'x' is a placeholder
GameObject newGameObject = gameObjectArray[x] as GameObject;
Instantiate(newGameObject, position, rotation);
//Code for Destroying the object:
Destroy(newGameObject);
//This results in the error...
Answer by meat5000 · Mar 30, 2015 at 11:53 AM
Sounds like you are trying to destroy an array of prefabs.
If you really want to do this, look up DestroyImmediate. It CAN and WILL destroy your assets permanently. YOU HAVE BEEN WARNED! :D
What you should be doing is creating a new variable to store the result of the Instantiate and destroy that handle instead.
GameObject myHandle = Instantiate ETC
Destroy(myHandle);
Answer by Fido7783 · Feb 02, 2021 at 10:24 PM
//this array is filled in the editor:
public GameObject[] gameObjectArray = new GameObject[0];
private GameObject newGameObject_clone;
//code for instantiating the object (this works fine):
//Note: 'x' is a placeholder
GameObject newGameObject = gameObjectArray[x] as GameObject;
newGameObject_clone = Instantiate(newGameObject, position, rotation);
//Code for Destroying the object:
Destroy(newGameObject_clone);
This is how I got around that.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer
Guided Missiles help? 2 Answers
PlayerRespawn class wont Instantiate the player prefab 1 Answer
Why won't this object destroy? 2 Answers