Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by pKallv, Apr 22, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    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:
    Code (csharp):
    1. void OnMouseDrag() {
    2.         //open a menu when the key has been held for long enough.
    3.         if((Time.time > downTime + waitTime)  !isHandled  mouseOver){
    4.             isHandled = true;// reset the timer for the next button press
    5.             //add markup menue script here.
    6.             Debug.Log ("MouseKey was pressed and held for over " + waitTime + " secounds.");
    7.  
    8.             // Check if multi-icon exist to be able to decide what to do
    9.             GameObject dummyGO = GameObject.FindWithTag("multi-icon");
    10.             print ("dummyGO: " + dummyGO);
    11.  
    12.             if(dummyGO == null) {
    13.                 theMultiIcon = Resources.Load("multi-icon") as GameObject;
    14.                 theMultiIcon.transform.position = new Vector3(3.2f, 4.5f, 1f);
    15.                 Instantiate(theMultiIcon);
    16.             }
    17.             else {
    18.                 Destroy(theMultiIcon);
    19.             }
    20.         }
    21.     }
    I have tried "DestroyImmediate (theObject, true)"

    Appreciate all help i can get :)
     
  2. AlfredDuler

    AlfredDuler

    Joined:
    Dec 22, 2012
    Posts:
    7
    Code (csharp):
    1.  if(dummyGO == null) {
    2.  
    3.                 theMultiIcon = Resources.Load("multi-icon") as GameObject;
    4.  
    5.                 theMultiIcon.transform.position = new Vector3(3.2f, 4.5f, 1f);
    6.  
    7.                 GameObject multiIconeInstantiated = Instantiate(theMultiIcon);
    8.  
    9.             }
    10.  
    11.             else {
    12.  
    13.                 Destroy(multiIconeInstantiated);
    14.  
    15.             }
    16.  
    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.
     
    krswoodward likes this.
  3. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    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:
    Code (csharp):
    1. if(dummyGO == null) {
    2.     var theMultiIconAsset = Resources.Load("multi-icon") as GameObject;
    3.     theMultiIcon = Instantiate(theMultiIconAsset, new Vector3(3.2f, 4.5f, 1f), Quaternion.identity);
    4. }
    5. else
    6. {
    7.     Destroy(theMultiIcon);
    8. }
    9.  
     
    pKallv and LutiDLutione like this.
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Thanks shaderop, it works :)
     
  5. loloel

    loloel

    Joined:
    Oct 6, 2019
    Posts:
    1
    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);
    }
    }

    }
     
  6. SharkieDesigner

    SharkieDesigner

    Joined:
    Jun 4, 2020
    Posts:
    1
    Hello, I need help with this error and it takes me to this line in the code but no errors in code are shown.
     

    Attached Files:

    • po1.JPG
      po1.JPG
      File size:
      63.5 KB
      Views:
      334
  7. krswoodward

    krswoodward

    Joined:
    Apr 30, 2020
    Posts:
    1
    I know it was 6 years ago but you just saved me so much hassle and lag.
     
    pKallv likes this.
  8. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Long time ago since I created this post indeed :)
     
  9. WooterWooter

    WooterWooter

    Joined:
    Mar 26, 2021
    Posts:
    1
    Pls help i am trying to destroy a particle effect prefab for a explosion like effect but cant through the error