Search Unity

Destroying assets is not permitted to avoid data loss.

Discussion in 'Scripting' started by Azux, Sep 15, 2019.

  1. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    Hi i read some previous threads but i don't understand what im doing wrong. So if I understood correctly i have to Destory a clone of prefab not prefab by itself . This is my code:
    Code (CSharp):
    1.     public void ChoseBuildingFromUI(int id)
    2.     {
    3.         Debug.Log(id);
    4.  
    5.         if(chosed_Building != null)
    6.         {
    7.             RemoveObjectFromGame();
    8.         }
    9.  
    10.         GameObject objectToBuild =(GameObject)Instantiate(buildings_all[id], Vector3.zero, Quaternion.identity);
    11.  
    12.         chosed_Building = objectToBuild;
    13.         chosed_building_rigid = chosed_Building.GetComponent<Rigidbody>();
    14.      
    15.  
    16.         Debug.Log("You've chosed building :" + buildings_all[id].name);
    17.     }
    18.  
    19.     public void  RemoveObjectFromGame()
    20.     {
    21.         chosed_building_rigid = null;
    22.         Destroy(chosed_Building);
    23.         chosed_Building = null;
    24.     }
    25.  
    So My chosed_Building = Cube(Clone) but still i get warning
    "
    Destroying assets is not permitted to avoid data loss.
    If you really want to remove an asset use DestroyImmediate (theObject, true);
    UnityEngine.Object:Destroy(Object)
    BuildMenu:RemoveObjectFromGame() (at Assets/Scripts/Modes/BuildMenu/BuildMenu.cs:94)
    BuildMenu:ChoseBuildingFromUI(Int32) (at Assets/Scripts/Modes/BuildMenu/BuildMenu.cs:79)
    <>c__DisplayClass12_0:<AddListenerToButton>b__0() (at Assets/Scripts/Modes/BuildMenu/BuildMenu.cs:69)
    UnityEngine.EventSystems.EventSystem:Update()"

    chosed_Building is public GameObject


    // It looks like i had one line in another script which after Input.GetMouseButtonUp(0) put again Cube(Clone) as chosed_building. So for now i fixed my problem Thread can be closed sorry.
     
    Last edited: Sep 15, 2019