Destroying assets is not permitted to avoid data loss. ??

I get the following message when trying to destroy a game object:

“Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true);”

Trying to learn and understand how to solve this and why this happen.

My code:

void OnMouseDrag() {
		//open a menu when the key has been held for long enough.
		if((Time.time > downTime + waitTime)  !isHandled  mouseOver){
			isHandled = true;// reset the timer for the next button press
			//add markup menue script here.
			Debug.Log ("MouseKey was pressed and held for over " + waitTime + " secounds.");

			// Check if multi-icon exist to be able to decide what to do
			GameObject dummyGO = GameObject.FindWithTag("multi-icon");
			print ("dummyGO: " + dummyGO);

			if(dummyGO == null) {
				theMultiIcon = Resources.Load("multi-icon") as GameObject;
				theMultiIcon.transform.position = new Vector3(3.2f, 4.5f, 1f);
				Instantiate(theMultiIcon);
			}
			else {
				Destroy(theMultiIcon);
			}
		}
	}

I have tried “DestroyImmediate (theObject, true)”

Appreciate all help i can get :slight_smile:

 if(dummyGO == null) {

                theMultiIcon = Resources.Load("multi-icon") as GameObject;

                theMultiIcon.transform.position = new Vector3(3.2f, 4.5f, 1f);

                GameObject multiIconeInstantiated = Instantiate(theMultiIcon);

            }

            else {

                Destroy(multiIconeInstantiated);

            }

Basically you have to Destroy the instance of the prefab, not the prefab itself. You better drag and drop the prefab in a public variable then instantiate it.

1 Like

Attempting to destroy an asset loaded through Resource.Load is like attempting to delete an asset (be it a prefab or a texture or what have you) from the Project view. It will delete that asset permanently if it were allowed, and it’s probably now what you want to do.

You should probably rewrite your code along the following lines:

if(dummyGO == null) {
    var theMultiIconAsset = Resources.Load("multi-icon") as GameObject;
    theMultiIcon = Instantiate(theMultiIconAsset, new Vector3(3.2f, 4.5f, 1f), Quaternion.identity);
}
else
{
    Destroy(theMultiIcon);
}
2 Likes

Thanks shaderop, it works :slight_smile:

Hello pKallv I have the same proble and below is my code do you think you can help me with it?

Really Appreciate the help
Thank you.

void SpawnQuestions()
{

Vector3 spawnpos = new Vector3(Random.Range(-spawnX, spawnX), 0, spawnPosZ);
int questionIndex = Random.Range(0, questions.Length);
Instantiate(questions[questionIndex]);

}
void QuestionOut ()
{
for (int i = 0; i < questions.Length; i++)
{
DestroyImediate(questions*.gameObject);*
}
}
}

Hello, I need help with this error and it takes me to this line in the code but no errors in code are shown.

I know it was 6 years ago but you just saved me so much hassle and lag.

1 Like

Long time ago since I created this post indeed :slight_smile:

Pls help i am trying to destroy a particle effect prefab for a explosion like effect but cant through the error