Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Destroying assets is not permitted to avoid data loss

Discussion in 'Scripting' started by getmyisland_dev, Jul 9, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    How would I have to change the
    Code (CSharp):
    1. Destroy(gameObject);
    code to avoid the error message in the title?

    I want to execute this code above in an void if that makes any difference and the gameObject I want to destroy is an prefab that's why I probably get the error message.

    Thanks in advance Max.
     
    Last edited: Jul 9, 2021
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Well, it tells you. Not permitted. Why are you destroying the prefab?
     
  3. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    I don't want to destroy the prefab, but the script does and I want to avoid this.

    Because if I write destroy(gameObject); it destroys the prefab.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Show your script because if you are doing a runtime script with Destroy(gameObject); in your script, it would be targetting the instance of the gameObject the script is on and not the prefab.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    In editor scripts you should be using Instantiate and DestroyImmediate on instantated objects. Otherwise you'd be destroying the asset itself and not a copy you've made with Instantiate.
     
  6. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    This is my complete script that is attached to my prefab:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerDeadBody : MonoBehaviour, IInteractable
    6. {
    7.     [SerializeField] Renderer bodyRenderer;
    8.  
    9.     public GameObject player;
    10.     public GameObject deadBodyPrefab;
    11.  
    12.     public float MaxRange { get { return maxRange; } }
    13.  
    14.     private const float maxRange = 10;
    15.  
    16.     public void SetColor(Color newColor)
    17.     {
    18.         bodyRenderer.material.color = newColor;
    19.     }
    20.  
    21.     void OnEnable()
    22.     {
    23.         PlayerManager playerManager = player.GetComponentInChildren<PlayerManager>();
    24.  
    25.         playerManager.allBodies++;
    26.     }
    27.  
    28.     public void Report()
    29.     {
    30.         Debug.Log("Reported");
    31.         Destroy(gameObject);
    32.     }
    33.  
    34.     public void OnStartHover()
    35.     {
    36.         //Debug.Log("OnStartHover called");
    37.     }
    38.  
    39.     public void OnInteract()
    40.     {
    41.         return;
    42.     }
    43.  
    44.     public void ReportDeadBody()
    45.     {
    46.         //Debug.Log("ReportDeadBody function called");
    47.         PlayerManager playerManager = player.GetComponentInChildren<PlayerManager>();
    48.         if (playerManager != null)
    49.         {
    50.             playerManager.ReportBody();
    51.         }
    52.         else
    53.         {
    54.             Debug.Log("PlayerManager not found");
    55.         }
    56.     }
    57.  
    58.     public void OnEndHover()
    59.     {
    60.         //Debug.Log("OnEndHover called");
    61.     }
    62. }
    Inside of the Report() void I want to destroy my gameObject, so that it disappears from my scene.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    How are you calling Report?
    If you instantiate your PlayerDeadBody, then make sure you target the instantiated object.

    For example, something like this. (may not be perfect code.)
    Code (CSharp):
    1. GameObject spawnedDeadBody = Instantiate(prefabDeadBody);
    2. spanwedDeadBody.GetComponent<PlayerDeadBody>().Report(); //Calls on the copy
    3.  
    4. prefabDeadBody.GetComponent<PlayerDeadBody>().Report(); //calling on the prefab
     
  8. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    This is not an Editor script. This script is on an gameObject that should be destroyed when
    Im currently handling it like this (in PlayerManager):
    Code (CSharp):
    1. spawnedDeadBody = Instantiate(bodyPrefab, transform.position, transform.rotation);
    2.         PlayerDeadBody tempBody = spawnedDeadBody.GetComponentInChildren<PlayerDeadBody>();
    And in the DeadBodyScript:
    Code (CSharp):
    1. PlayerManager playerManager = player.GetComponentInChildren<PlayerManager>();
    2.  
    3.  
    4.         if (playerManager)
    5.         {
    6.             Debug.Log("Reported");
    7.             Destroy(playerManager.spawnedDeadBody);
    8.         }
    Now the gameObject gets not deleted anymore and nothing happens