How to save game objects?

I have a list which saves inputed values from the user. I want to be able to save this list even when the user goes to a different screen and returns or exits the game completely and returns. I have two game objects I want saved, GradeDisplay and ListDisplay. The GradeDisplay shows the average amount of points based on what the user has entered into the list and the ListDisplay shows the actual list of what numbers the user has entered. How do I save these two game objects?

If what you is a save load system, then I suggest Json. Don’t be intimidated by the script it’s mostly comments.
Here’s an example script that saves a score:

     private void Start()
     {
         //A class with a bunch of static vars
         if(!gameManager.loaded)
             Load();
         //Checks if the curret high score is greater then the previus high score 
        //and saves it
         if (gameManager.CurHighScore > gameManager.HighScore)
             Save();
     }
 
     void Save()
     {
         //Creates a var of the score class created below
         Score scoreSaved = new Score
         {
             score = gameManager.CurHighScore
         };
 
         //Converts the scoreSaved var created above, to json
         string json = JsonUtility.ToJson(scoreSaved);
 
         //Saves the json to a text file called "saveScore" in the same folder
         File.WriteAllText(Application.dataPath + "/saveScore.txt", json);
         Debug.Log("SAVED");
     }
     void Load()
     { 
         //Tells the gameManager that we have loaded the high score to avoid 
         //bugs
         gameManager.loaded = true;
 
         //Converts the text file to Json
         string json = File.ReadAllText(Application.dataPath + "/saveScore.txt");
 
         //Converts the Json file to a variable of type Score
         Score scoreSaved = JsonUtility.FromJson<Score>(json);
 
         //Updates the gameManager's high scores
         gameManager.HighScore = scoreSaved.score;
         gameManager.CurHighScore = scoreSaved.score;
 
         Debug.Log("LOADED");
     }
 
     //The class that stores score
     //Must add System.Serializable to the class so we can save it
     [System.Serializable]
     public class Score
     {
         public int score;
     }

Make sure you add this to the top of the script so that the game knows how to read the script

 using System.IO;

And heres the gameManager:

 public static class gameManager
 {
     public static bool loaded = false;
 
     public static int score = 0;
     public static int HighScore = 0;
     public static int CurHighScore = 0;
 }

I can help you make it work for your game, if you wan’t

@Aadee123 - as @Deuce2008 said, you can make a save/load system, or if the amount of data you want to save is small and not that important ( collectable items, in game money, etc. ) you can use PlayerPrefs

Another way that I like to use for smaller items along with player preffs (Saved in appdata and easily referenced, not recommended for hard save data but good for basic active data and of course player preferences like volume control and the like) is Scriptable Objects.


This allows you to create an asset that will not be affected by unity editor or crashes. And being a built in asset it will not be easily messed with. (Though the same could be said of any referenced item).

I would also recommend adding some functions to clear data or “reset” the profile and would consider putting a cap if you’re going to be just adding to a list. Hope this helps =)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// The way this script works is by adding the tool to unity.
/// From Unity you can create the object and it will hold and save any data it has.
/// Being an object it can be referenced easily by any number of items and they will 
/// -get the data from the same object allowing you to get data across scenes easily.
/// Also works for active data.
/// </summary>
[CreateAssetMenu(fileName = "New MyScriptableObject")] //adds item to the right click create asset option within Unity
public class MyScriptableObject : ScriptableObject //ScriptableObject lets you make a saved object with the data
{

    /// <summary>
    /// Create any lists or asset types you desire here.
    /// Making them public will make them editable in inspector like normal.
    /// </summary>

    public string demoName = "Name"; //stores sting
    public float demoNumber = 1; //stores number(Float)
    public List<float> floatList; //This totally works for a growning data type

    public void Print() //even though its a data object you CAN still create and add functions to be referenced and used.
    {

        Debug.Log("Button has been pushed");
        Debug.Log(demoName);
        Debug.Log(demoNumber);
        Debug.Log(floatList);
        floatList.Add(floatList.Count);

    }

}