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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

memorize objects or actions at scene change

Discussion in 'Scripting' started by ThePain1987, Jun 20, 2022.

  1. ThePain1987

    ThePain1987

    Joined:
    Mar 25, 2020
    Posts:
    9
    good morning everyone, i was thinking of a way to save the objects and actions performed at the change of scene.
    i have already prepared the script for the scene change, nothing complicated for now, very simple. a collider, which loads the new scene when contact is made.
    i wanted to understand the way that when you go back to the previous scene, the collectibles (for example the collected coins), or the enemies killed do not appear when loading the new scene.
    can you direct me to the best way to do this. i have seen the playerprefs, but i don't know if it is the best solution.
    Thank you
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    I'll move your post to the scripting forum as it doesn't relate to 2D features.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    Any type of inter-scene "memory" is typically handled by a GameManager construct. There's a lot of tutorials on Youtube for the details and concept.

    If you understand the concept of a GameManager, here's two usable examples:

    ULTRA-simple static solution to a GameManager:

    https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    OR for a more-complex "lives as a MonoBehaviour" solution...

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.
     
  4. ThePain1987

    ThePain1987

    Joined:
    Mar 25, 2020
    Posts:
    9
    thank you very much kurt, yes, I have the concept of the game mager in mind and for now I have done something simple. I use it for the management of collectibles, for example coins, where if the character takes them, he accumulates them, and with the change of scene the total. I have no problem with that.

    I was thinking about how to keep track of which coin (for example) was taken.
    for example scene1 I take the coin, I change scene and the total is 1. up to here ok. from scene2 I go back to scene1, and with the loadscene reload the coin, when once taken it is no longer possible to take it back.

    what i thought is that in the gamemanager i kept a list, where at loading (at awake or at start) if that coin is in my list, i destroy it.
    I don't know if this is a good idea.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    Ah, sounds like loading / saving, aka "persistence."

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide