Using PlayerPrefs

I have recently been creating a game with tutorials. Unfortunately, they didn’t cover a save score feature. Thanks to another user, I was able to figure out that I needed to use playerprefs. I watched tutorials online, but none of them were helpful. If you can, please help me!

Tutorial for the game: Clicker Game Tutorial Unity C#, Part 1 - YouTube

Gold Per Sec Script:

using UnityEngine;
using System.Collections;

public class GoldPerSec : MonoBehaviour {

    public UnityEngine.UI.Text gpsDisplay;
    public Click click;
    public ItemManager[] items;

	void Start () {
        StartCoroutine(AutoTick ());
	}
	
	void Update () {
        gpsDisplay.text = GetGoldPerSec() + " Money Per Sec";
	}

    public float GetGoldPerSec() {
        float tick = 0;
        foreach (ItemManager item in items) {
            tick += item.count * item.tickValue;
        }
        return tick;   
    }

    public void AutoGoldPerSec() {
        click.gold += GetGoldPerSec() / 10;
    }

    IEnumerator AutoTick() {
        while (true) {
            AutoGoldPerSec();
            yield return new WaitForSeconds(0.10f);
        }
    }
}

Gold Per Click script:

using UnityEngine;
using System.Collections;

public class Click : MonoBehaviour {

    public UnityEngine.UI.Text gpc;
    public UnityEngine.UI.Text goldDisplay;
    public float gold = 0.00f;
    public int goldperclick = 1;
	
	void Update () {
        goldDisplay.text = "" + gold.ToString("F0");
        gpc.text = "Money Per Click: " + goldperclick;
	}

    public void Clicked(){
        gold += goldperclick;
    }
    
}

My idea was for the game to save when the game is quit, and load as soon as you load the game back up. I am a complete beginner, if anyone can tell me how to do this, please tell me!
Thanks! :smiley:

Please note that PlayerPrefs is an easy way to save data but also an very unsafe way.
The player can easily manipulate his “goldValue” since it’s just stored as an integer in some file on his device. PlayerPrefs should usually just be used for values the player can changed any way within in game, like volume setting etc. .
If you wanna be on the (pretty) safe side, you should look into this:

Example Code:

     void Save()
    {
        string filename = "/filename.dat";
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath+filename);
        bf.Serialize(file, goldValue); //Use can easily use e.g. a List if you want to store more date
        file.Close();
    }

     bool Load()
    {
        string filename = "/filename.dat";
        if (File.Exists(Application.persistentDataPath + filename))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + filename, FileMode.Open);
            goldValue=(int) bf.Deserialize(file); 
            file.Close();
            return true;
        }
        return false;
    }

Where have you declared a new playerpref variable to save the amount of gold to upon game save and retrieve it from upon game load?

Apologies if I’ve missed this.

Here is the line of code you need to store and retrieve score from PlayerPrefs:
PlayerPrefs.SetInt (“Gold”, goldValue);
PlayerPrefs.GetInt (“Gold”);

where “Gold” is a unique key to manage data in PlayerPrefsand ‘goldValue’ is the variable which hold the value of gold OR else you can directly pass the value as parameter as well.

For more details about PlayerPrefs please refer: Unity - Scripting API: PlayerPrefs