Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Retaining object state

Discussion in 'Scripting' started by Seaworth, Aug 21, 2019.

  1. Seaworth

    Seaworth

    Joined:
    Aug 15, 2019
    Posts:
    19
    Hello folks,

    May I ask for some help regarding the maintenance of object state between scenes. If I leave scene x with the barn door broken (therefore doorimage = "brokendoor") two buttons on the stove pressed down and one button inactive (topleft = "pressed", bottomleft = "pressed", topright="inactive") and body on floor at a given spot. I move on to scene y, scene z, scene y, scene a and then back to scene x, I want scene x to appear as it did when I left it and not in it's original editor created condition. In other words, store object statuses just before I leave scene x, move away, restore object statuses as I return to scene x and then enter a scene x no different from the one I left earlier.

    I suspect I am waffling. It's a stress response. To me this seems to be something fundamental in a game, yet I see no obvious way of doing it.Please help.

    Regards to all
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,544
    Last edited: Aug 21, 2019
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    877
    Well, the cleanest solution is to save ("persist") the Status of your objects when you Exit the Scene, and load it when you enter it, substituting Default values when None are saved.

    To save info, there are multiple methods, the simplest (for proof of concept), use PlayerPrefs.
     
  4. Seaworth

    Seaworth

    Joined:
    Aug 15, 2019
    Posts:
    19
    gh

    Thanks. I'll look into it. It seems that PlayerPrefs is really a bad name though, because it implies less than things like my situation and suggests it is only about, well, player prefs and perhaps things like ammo or gold, but not open windows.
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    877
    Indeed. PlayerPrefs is only to make a proof of concept. A much better way is using a dictionary, and save it as JSON to a temp Directory. Using PlayerPrefs is just a short hack that allows you to verify your idea holds water.
     
    Seaworth likes this.
  6. Seaworth

    Seaworth

    Joined:
    Aug 15, 2019
    Posts:
    19
    I saved the info to a JSON file, but could not find a method during which I could read the data back before opening the scene. By the time it got to start it was too late, unless the scene was being loaded for the first time/ And you don't want to save and exit before coming back to the scene just to make sure the object data persists.
     
  7. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    877
    Ah. Now there's a dirty little trick you can use: save the JSON string to an object that has 'dontDestroyOnLoad' set, so it will persist though scene loads, and read the existing string on start. If it's not present, load default values.
     
    Joe-Censored likes this.