Search Unity

Keep int values between scenes (and edit them in the second scene)

Discussion in 'Editor & General Support' started by BlueBoyTech, Apr 20, 2018.

  1. BlueBoyTech

    BlueBoyTech

    Joined:
    Mar 26, 2018
    Posts:
    5
    Let's say I have two scenes, Scene #1 and Scene #2. I have an Empty Object named ScriptObject which contains a script with certain integer values I'd like to use in Scene #2. I tried using DontDestroyOnLoad for the ScriptObject however while the values are retained I can't edit or even touch them in Scene #2 because when I switch to Scene#2 ScriptObject isn't present yet because I haven't loaded Scene#1 yet. How would I do this?
     
  2. poisonnuke

    poisonnuke

    Joined:
    Nov 22, 2016
    Posts:
    82
    take a look at singleton-pattern. If you have a class with a static intance of itself and a static getter to that instance, then you can access this class (which doesnt need to derive from monobehavior, and must not be attached to a gameobject) from every script, regardless which scene is loaded.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah I do a singleton like pattern for things like this. I generally will have a prefab set to DontDestroyOnLoad that I use to store this kind of thing. When I load a scene I do a FindGameObjectWithTag to find the object, if the object exists I cache the reference, if it doesn't exist I instantiate it.
     
  4. BlueBoyTech

    BlueBoyTech

    Joined:
    Mar 26, 2018
    Posts:
    5
    Could you write an example? This is confusing me a bit.
     
  5. BlueBoyTech

    BlueBoyTech

    Joined:
    Mar 26, 2018
    Posts:
    5
    Nevermind, I figured it out!
     
  6. poisonnuke

    poisonnuke

    Joined:
    Nov 22, 2016
    Posts:
    82
    a bit late, but for all others looking for an example:

    Code (CSharp):
    1.  public class GlobalManager {
    2.         #region Singleton
    3.         private static GlobalManager _instance;
    4.         private static object _thisLock = new object();
    5.  
    6.         public static GlobalManager Instance {
    7.             get {
    8.                 lock (_thisLock) {
    9.                     if (_instance == null) {
    10.                         _instance = new GlobalManager();
    11.                     }
    12.                 }
    13.                 return _instance;
    14.             }
    15.         }
    16.  
    17.         #endregion
    18.  
    19.         //all the methods and accessors and properties and so on
    20. }
    21.  
    now just access the Instance property from any script you want, which you should store in inside the script, to avoid the semaphore on every call.
     
    Last edited: Apr 25, 2018
  7. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Or instead of the nasty singleton pattern, use ScriptableObject
    simply make an scriptable object holding an integer value.
    You can simply reference that integer anywhere where you want, change it and its value will persist through scenes.
     
  8. poisonnuke

    poisonnuke

    Joined:
    Nov 22, 2016
    Posts:
    82
    oh yeah... an abstract class to derive from with singleton inside. It is exactly the same, the only difference is, that accessor and constructor are looking a bit different. Except that, there is no benefit or difference at all.
     
  9. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    You're creating extra dependencies which kills modularity. I'd say that is a big benefit you're losing.
    With the singleton pattern you make things heavily depend on eachother to be there.
    With a scriptable object you can just insert anything in there, as the script that uses that scriptable object doesn't care where it comes from. It just uses that value.

    A scriptable object doesn't have to be more than

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Integer Variable")]
    4. public class IntegerVariable : ScriptableObject
    5. {
    6.     public int Value;
    7. }
    Create the asset file, link it up to where ever you need or change it.

    Have a look at this:
     
    Last edited: Apr 27, 2018