Search code examples
c#unity-game-enginesavegame-development

I am looking for a way to save across scenes(i need to save whether a level has been completed to unlock the next level on the menu)


I am struggling with this and can't find a tutorial online, i need the game to know whether the tutorial has been completed when returning to the menu so i can unlock the "level one" button, but every time i change scenes everything resets. all i need is a way to make the scenes not reset and talk to eachother about what has happened in other scenes. is there a way to store data that can be accesed by all scenes?

I have looked at some tutorials but they dont seem to help.


Solution

  • There are number of ways to save and load data. The easiest is to save data in PlayerPrefs

     PlayerPrefs.SetInt(KeyName, Value); // for saving an int value
     PlayerPrefs.SetString(KeyName, Value); // for saving a string value
     PlayerPrefs.GetString("KeyName"); // for reading a saved string
     PlayerPrefs.GetInt("KeyName"); // for reading a saved int
    

    If the data you want to save and read is bit more complex, I'd suggest using Scriptable Objects. But please be aware that the data saved and read from Scriptable Objects is only accessible in Editor. In order to truly harness the power of Scriptable Object you would have to serialize the data container created with Scriptable Object and write to a file and restore it from the file when needed.

    Do note that these values get reset when the application data is cleaned or application is removed and reinstalled. For saving data across multiple devices or to ensure that data is not lost with device you may save it on your own server and access it using REST APIs.

    This requires you to create web services for handling REST API. Look into Unity Web Request for how you can implement it.