Search Unity

What is the best way to save player current health and inventory items when you load a new scene?

Discussion in '2D' started by Pixitales, Jun 3, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    What is the best way to save player current health and inventory items when you load a new scene? i.e entering and exiting a house.... Would it be better to use Dont destroy on load, make a static class, scriptable object or playerprefs? I certainly don't want to use Json to save and load everytime I enter or exit a house.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,466
    PlayerPrefs is easiest
     
  3. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    I would go for a mix with two approaches.
    Using a script with DontDestroyOnLoad for storing all the data of the player, and using this one to store and retrieve the info between loading scenes inside a game session. And if you need to store the info between game sessions (user closes and reopen the game), then I would use player prefs.
    Using a script to store data is more difficult at the beginning because you have to create it, but later will get easier than storing and retrieving all the data from player prefs each time you need to enter a house (like you said).
    And you can even create a utility function inside this same script to store and retrieve all the data when the user closes or reopen the game.
     
  4. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    yeah, it seems most people say DontDestroyOnLoad is to way to go. Apparent I have to attach a different scriptable object for each scene on my player, so DontDestroyOnLoad doesn't work for me.
     
  5. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    You don’t need to use DontDestroyOnLoad on your Player, you can create an auxiliary object to hold these script and use it’s reference in the player script.