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);
}
}
}
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.
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);
}