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

Scriptable Objects Lifetime

Discussion in 'Editor & General Support' started by lrasomattos, Feb 8, 2019.

  1. lrasomattos

    lrasomattos

    Joined:
    Aug 22, 2015
    Posts:
    29
    Hi! I am very confused about how people have been using SOs and how they behave in memory (load/unload/stay).

    --- Some considerations first (you can jump to the questions) ---

    I know editing a SO in Play Mode or Edit Mode, in Editor, will always change the Asset file.
    I know that editing a SO runtime in a build will never change the Asset file.

    BUT there is huge inconsistency in what people are saying in Unity Forums, Answers, Tutorials and even Unite Talks about how they behave in memory.

    The only thread I found that actually touches these subjects more clearly is this:
    https://forum.unity.com/threads/scriptableobject-life-time-object-identity-and-loading.508969/

    It is a nice thread, but a really long read (that I went through) and although I did learn a lot there, it's mostly theories and they end up deviating into other subjects. So I will try to be more direct in the questions here.

    I know it is a big text sorry, but it really seem like this information is not clear or lacking. And it may have a big impact for some designs I have seen. In my game, for example, we use SOs for all sound effects and their configurations (random volume/pitch, random clips), if they stay in memory forever it can be a huge leak. If not, many people who are using them, believing in their state consistency during a play, can find (maybe randomly) problems as SOs are unloaded from memory.

    --- So, after all, here are the questions ---
    • Are SOs forever loaded (or until explicitly unloaded) in memory during a session in a build?
      • and If yes
        • is there an explicit way of unloading them?
      • and If no
        • When does it happen? (randomly/before,during,after changing scene/on any scene load/when gc is called)
        • Why does it happen, what are the requisites? (not being referenced, too large, whatever)
        • any ways to keep the object alive?
        • do their internal states persist, somehow, for the whole session (like saved in disc or smth)?
          • and if no, does it means that when Ryan Hipple shows how to use/make scriptable variables and "singletons" in his Unite Austin 2017 talk, he is actually in risk to lose all the objects and managers states when SOs are unloaded? - He might have talked about it but I watched it many times and can't remember.
    • How do Assets referenced by an SO behave:
      • Are they also loaded to memory together with the SO?
      • Are they unloaded together with the SO?
    • If SOs can be unloaded, does it mean that there may be different instances of SOs created during a play? Can objects be ever referencing different instances of the same SO?
     
  2. roboryantron

    roboryantron

    Joined:
    Apr 2, 2014
    Posts:
    5
    Scriptable Objects may be unloaded when there is nothing referencing them. You can set the hide flags to DontUnloadUnusedAsset to make sure it is kept in memory.