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 Do I need to destroy Instantiated ScriptableObjects not used anymore?

Discussion in 'Scripting' started by Magnilum, Sep 4, 2023.

  1. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    143
    Hello, I am currently creating a tool for Unity and I am using ScriptableObjects to save some data. In order to preserve the original data, I Instantiate a new ScriptableObject from this to have a copy that I can modify then save if needed.

    Code (CSharp):
    1. Settings originalSettings;
    2. Settings currentSettings;
    3.  
    4. currentSettings = Object.Instantiate(originalSettings);
    5.  
    But, when it comes to clear the currentSettings, I am doing this:

    Code (CSharp):
    1. currentSettings = null;
    Should I destroy it before to free memory space or Unity do it itself?

    Code (CSharp):
    1. DestroyImmediate(currentSettings):
    2. currentSettings = nul;
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Yes, you should destroy objects you instantiate. Those objecty would be destroyed in rare cases when no references to it are hold anymore. However that only happens when you load a new scene or when Resources.UnloadUnusedAssets is called. Any object derived from UnityEngine.Object that you create / clone has to be destroyed by you. This include Texture2Ds, Meshes, Materials, GameObjects and other objects. Note that destroying a gameobject / gameobject tree will destroy all components attached to those gameobjects automatically.

    Any UnityEngine.Object derived object is actually "tracked" on the native C++ side. You can actually find those again with FindObjectsOfType even when all references have been wiped.

    ps: Note that you should be careful with DestroyImmediate. It's mainly used by editor code. At runtime you should use Destroy in 99% of all cases.
     
    Magnilum likes this.
  3. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    143
    Thank you for this answer, I will do it right now !
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Just a word to the wise, destroying Scriptable Objects can cause problems:(TimeStamp 18:05)


    I would just re-write that scriptable objects data, unless that can't be done, then I'd just use a static class. :)
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    It's only a problem if you don't null out any references to the SO.

    Ideally instanced SO's would only be used in a small scope so there aren't dozens of references to them.

    That said, instancing SO's always feels heavy handed to me, and in most cases would be better served by plain C# classes, with a potentially a scriptable object that can act as a factory for these plain objects.
     
    Bunny83 and wideeyenow_unity like this.