Saving Data as a modifiable asset

Alright, so I’ve made a lot of progress learning, and I’m having a lot of fun, but I’ve spent the past 6 hours trying to find a way to accomplish this, and I just can’t find anything that seems to resemble what I’m looking for.

So I have a stat tracker for a sport. You click buttons, it increases variables, you have a timer, etc. It’s great fun and works quite well. What I’m needing now is the ability to, when I’m done tracking stats for a game, I save the stats to a new asset (or something similar). I then need to be able to access the variables in that asset to modify, and I also need to display that information in a list of the games on the main screen. (The main screen issue I’ll tackle at another time.)

(In the following paragraph, “Game” refers to a sports game)
I figured the best way to do this would be, when the save button is clicked, to save the latest games info into a Scriptable Object name Game. Then I would load that information back into the screen if I chose to edit the game. However, I can’t find anywhere how to take variables, save their current state, and store that in an asset. I also briefly looked into dictionarys, but they seem a bit too manual in the terms of their creation. One of the most difficult parts to get around, is that, I need to be able to save and access the data, but I also need to be able to have multiple different games available. So that when I save the new game, it doesn’t overwrite the previous one’s filepath. Another way seemed to be playerprefs, but from what I can tell that is more for saving a games information, and not used for multiple inputs.

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

public class MakeAndMissButton : MonoBehaviour {


    public TMP_Text totalPoints;

    public TMP_Text goalPointsText;
    public TMP_Text savePointsText;
    public TMP_Text assistPointsText;
    public TMP_Text stealPointsText;

    public TMP_Text goalMakeText;
    public TMP_Text saveMakeText;
    public TMP_Text assistMakeText;
    public TMP_Text stealMakeText;
    public TMP_Text goalMissText;
    public TMP_Text saveMissText;
    public TMP_Text assistMissText;
    public TMP_Text stealMissText;

    public int totalScore;

    public int goalMake, goalMiss, goalScore, goalDisplay;

    public int saveMake, saveMiss, saveScore, saveDisplay;

    public int assistMake, assistMiss, assistScore, assistDisplay;

    public int stealMake, stealMiss, stealScore, stealDisplay;

    public string goalFinal;
    public string saveFinal;
    public string assistFinal;
    public string stealFinal;
    private int total;

    public void Stat(string name)
    {
        if (name == "goal")
        {
            goalFinal = "You earned " + goalPointsText.text + " points with goals or free kicks." +
                "You made " + goalMakeText.text + " goals, and only missed " + goalMissText.text + ". Great Work!";
            Debug.Log(goalFinal);
        }
    }

    public void Update()
    {
        totalPoints.text = ("PWP Points: " + total.ToString());

        total = goalDisplay + saveDisplay + assistDisplay + stealDisplay;
        goalPointsText.text = goalDisplay.ToString();
        savePointsText.text = saveDisplay.ToString();
        assistPointsText.text = assistDisplay.ToString();
        stealPointsText.text = stealDisplay.ToString();

        goalMakeText.text = goalMake.ToString();
        saveMakeText.text = saveMake.ToString();
        assistMakeText.text = assistMake.ToString();
        stealMakeText.text = stealMake.ToString();

        goalMissText.text = goalMiss.ToString();
        saveMissText.text = saveMiss.ToString();
        assistMissText.text = assistMiss.ToString();
        stealMissText.text = stealMiss.ToString();

        Dictionary<string, string> stats = new Dictionary<string, string>();
        //DICTIONARY STUFF. DONT KNOW ENOUGH RIGHT NOW. LEARN MORE
        stats.Add("1", "a");
        stats.Add("2", "b");
        stats.Add("3", "c");
        stats.Add("4", "d");
        stats.Add("5", "e");
        stats.Add("6", "f");
        stats.Add("7", "g");
        stats.Add("8", "h");
        stats.Add("9", "i");
        stats.Add("10", "j");
        stats.Add("11", "k");

    }


    //Goal Amount increase and decrease
    public void IncreaseMakeGoal(int goalWorth)
    {
        goalDisplay += goalWorth;
        goalMake++;
    }
    public void DecreaseMakeGoal(int goalWorth)
    {
        goalDisplay -= goalWorth;
        goalMake--;
    }
//Saves amount increase and decrease
    public void IncreaseMakeSaves(int saveWorth)
    {
        saveDisplay += saveWorth;
        saveMake++;
    }
    public void DecreaseMakeSaves(int saveWorth)
    {
        saveDisplay -= saveWorth;
        saveMake--;
    }
//Assists amount increase and decrease
    public void IncreaseMakeAssists(int assistWorth)
    {
        assistDisplay += assistWorth;
        assistMake++;
    }
    public void DecreaseMakeAssists(int assistWorth)
    {
        assistDisplay -= assistWorth;
        assistMake--;
    }

    public void IncreaseMakeSteals(int stealWorth)
    {
        stealDisplay += stealWorth;
        stealMake++;
    }
    public void DecreaseMakeSteals(int stealWorth)
    {
        stealDisplay -= stealWorth;
        stealMake--;
    }

    //Goal Amount increase and decrease
    public void IncreaseMissGoal()
    {
        goalMiss++;
    }
    public void DecreaseMissGoal()
    {
        goalMiss--;
    }
    //Saves amount increase and decrease
    public void IncreaseMissSaves()
    {
        saveMiss++;
    }
    public void DecreaseMissSaves()
    {
        saveMiss--;
    }
    //Assists amount increase and decrease
    public void IncreaseMissAssists()
    {
        assistMiss++;
    }
    public void DecreaseMissAssists()
    {
        assistMiss--;
    }

    public void IncreaseMissSteals()
    {
        stealMiss++;
    }
    public void DecreaseMissSteals()
    {
        stealMiss--;
    }
}

So, using the code above (I’m aware I might be way overdoing the code here, but it’s working well from what I can tell), I want to save the stats (goals, saves, etc) to be able to access and edit/load in the future, while also being able to have multiple saved games at one time.

These are pretty much your options. Scriptable objects really don’t work for that purpose.

1 Like

Thanks for showing me that. I’m currently about 20 minutes in, and I haven’t seen anything very useful. However, one of the suggested videos is about xml files, so maybe that will work. regardless, I will finish this video first.

I would look into json. Unity has their own json serializer and honestly, to me it’s easier to understand than xml.