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

ScriptableObject persistance across scenes without reference

Discussion in 'Scripting' started by CMDi-admin, Jan 30, 2018.

  1. CMDi-admin

    CMDi-admin

    Joined:
    Jul 23, 2013
    Posts:
    15
    Hi,

    I'm currently having an issue with my scriptable objects which store data, however not all scenes should be referencing this data, but on returning to the original scene, I would expect the data to be still stored in the scriptable object, which is not the case currently.

    For example:
    I'm storing an string based ID on a StringVariable object which derives from ScriptableObject, this ID's input comes from the user in the login scene. After going to another scene which doesn't need to know about the ID, and thus doesn't have a reference to it, I'm going to back to a scene (could be the login again) which does need the ID. At this moment the ScriptableObject has lost it's data and is reset to the default value.

    This all does work if the next scene is also referencing the same object. Is there an easy fix for this, or should I just put those variable on a general PersistantData object and keep that one in all my scenes?
     
  2. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    I think Scriptable objects are not supposed to store data across scenes like that. They are for storing data on the editor, as in store before building, not at runtime. When in runtime they are kind of like any another class.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can get them to save data across scenes, of course. I do not remember the method off-hand, I'd have to try to search for it. (I suspect it wasn't saved correctly, is what I mean. They keep their data because their assets.)

    However, that point doesn't matter at all, because that won't work in a build like that, so you should avoid it now to begin with :)

    You should store the string variable in a regular class/script.
     
    whileBreak likes this.