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

Question Destroy!

Discussion in 'Scripting' started by JimWebs, Oct 8, 2023.

  1. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    I'm working on something which creates a lot of objects on the fly, attaching triggers to the objects etc, which I keep track of through a dictionary. When I've done with them, I destroy them, and remove them from the dictionary, instead of leaving them just sitting there. Being new to unity, I wonder if Destroy removes listeners, triggers, everything ... and is it really necessary to destroy materials and objects? Sometimes I get a message saying destroy shouldn't be used or is not allowed.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,393
    If you destroy a gameobject all components should also be destroyed.
    C# listeners within the object probably need to be removed manually
     
    JimWebs and Bunny83 like this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,843
    You only really need to destroy non-game object/component Unity objects that have been instanced on the fly. Such as scriptable objects you've made during runtime with
    ScriptableObject.CreateInstance
    , or materials that have been instanced by accessing a renderer's
    material(s)
    property.

    You only need to unsubscribe from listeners/delegates if those objects are not also being destroyed.
     
    JimWebs, CodeRonnie and DevDunk like this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    Please describe that in detail, when and where you get that message, the code that triggers it including the event method it is called from, and the exact error message.

    These describe real problems, even if they may appear only as "warning" messages.
    You also need to differentiate between editor scripts respectively components executing in edit mode, and playmode/runtime code. For example in editor code you're supposed to use DestroyImmediate but at runtime only Destroy should be used.
     
    JimWebs likes this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    What's the exact message?
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    "Jimmy, this is your great great grandpa speaking from the other side. Please stop destroying things.. Thanks. Bye."
     
    JimWebs, CodeRonnie and Kurt-Dekker like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Mostly there's two things you can't destroy at runtime:

    - You cannot destroy a Transform or RectTransform

    Can't destroy Transform component of 'GameObject'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.


    - You cannot destroy Prefabs

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


    If you see a message, it's almost certainly one of those.

    Similarly you can't start an Animator on a prefab, but the message is slightly mysterious:

    Animator does not have an AnimatorController


    Same goes for regular Animations and ParticleSystems. Just think about what you're trying to do and it makes sense that you cannot do it. It's an asset on disk, not live in memory.

    Scripts to see for yourself:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // @kurtdekker - the main things you cannot destroy
    4.  
    5. public class CannotDestroy : MonoBehaviour
    6. {
    7.     [Header( "Drag a prefab asset reference in here.")]
    8.     public GameObject ThePrefab;
    9.  
    10.     void Start ()
    11.     {
    12.         Debug.Log( "Preparing to attempt destroying my own Transform.");
    13.         Destroy(this.transform);
    14.  
    15.         Debug.Log( "Preparing to attempt destroying a prefab.");
    16.         Destroy(ThePrefab);
    17.     }
    18. }
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // @kurtdekker - you cannot animate a prefab on disk
    4.  
    5. public class CannotAnimate : MonoBehaviour
    6. {
    7.     [Header( "Drag a prefab with Animator in here.")]
    8.     public Animator PrefabAnimator;
    9.  
    10.     void Start ()
    11.     {
    12.         PrefabAnimator.Play("Foo");    
    13.     }
    14. }
     
    JimWebs, Bunny83 and Ryiah like this.
  8. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    Lol what :) But I like destroying things, makes me feel as if I cleaned up
     
  9. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    Well it hasn't happened in a while but when it does (which I'm sure it will again, since my approach to coding is scatterbrained) then I'll post.