Struggling to save player score

I’ve very little to no knowledge and just starting out, im struggling on saving player score via playerprefs, yes I’ve watched plenty of tutorials and read other forum posts but i dont get it!!
Someone gave me a script but i have no ides where to put anything, can someone please help me? Im not good at this at all!!
First script is my resources script
Second is the playerpref script i was given with 3func

using UnityEngine;

using UnityEngine.UI;


public class ResourcesCastle : MonoBehaviour {


    public int Coins;

    public int Experience;

    public int LevelCastle = 1;

    public int Wave = 1;


    public float Timers = 120f;


    public GameObject Cl1;

    public GameObject Cl2;

    public GameObject Cl3;


    public Text CoinsText;

    public Text ExperienceText;

    public Text LevelCastleText;

    public Text WaveText;

    public Text Timer;


    private float times;


    private bool Wave2 = false;

    private bool Wave3 = false;

    private bool Wave4 = false;

    private bool Wave5 = false;


    void Start()

    {

        times = Timers;

    }


    void Update()

    {


        WaveUp();

        Texts();

        Waves();

        LvlCastle();


    }


    void WaveUp()

    {

     

        if(Wave5 == true)

        {

            Wave = 5;

        }


        if(Wave == 2 && Wave2 == false)

        {

            gameObject.GetComponent<EnemySpawn>().TimerMax -= 2;

            Wave2 = true;

        }


        if(Wave == 3 && Wave3 == false)

        {

            gameObject.GetComponent<EnemySpawn>().TimerMax -= 3;

            Wave3 = true;

        }

        if(Wave == 4 && Wave4 == false)

        {

            gameObject.GetComponent<EnemySpawn>().TimerMin -= 1;

            gameObject.GetComponent<EnemySpawn>().TimerMax -= 4;

            Wave4 = true;

        }

        if (Wave == 5 && Wave5 == false)

        {

            gameObject.GetComponent<EnemySpawn>().TimerMin -= 1;

            gameObject.GetComponent<EnemySpawn>().TimerMax -= 5;

            Wave5 = true;

        }

    }


    void Texts()

    {

        CoinsText.text = Coins.ToString();

        ExperienceText.text = Experience.ToString();

        LevelCastleText.text = LevelCastle.ToString();

        WaveText.text = "Wave:" + Wave.ToString();

    }


    void Waves()

    {

        times -= Time.deltaTime;

        if(times <= 0)

        {

            Wave += 1;

            times = Timers;

        }

    }


    void LvlCastle()

    {

        if (Cl1.activeInHierarchy == true)

        {

            LevelCastle = 1;

        }

        if (Cl2.activeInHierarchy == true)

        {

            LevelCastle = 2;

        }

        if (Cl3.activeInHierarchy == true)

        {

            LevelCastle = 3;

        }

    }
private int coins;

private string coins_key = "coins";

 

public void Save()

{

    PlayerPrefs.SetInt(coins_key, coins); // 1

    PlayerPrefs.Save(); // 2

}

 

public void Load()

{

    if (PlayerPrefs.HasKey(coins_key)) // 3

    {

        coins = PlayerPrefs.GetInt(coins_key); // 4

    }

}

 

public void Clear()

{

    PlayerPrefs.DeleteKey(coins_key); // 5

    PlayerPrefs.Save(); // 2

}

Hello Zaros,

PlayerPrefs is a nice and simple way of storing values between scenes and sessions, so you are going the right way.
PlayerPrefs has two main functionalities:

  • Set - To save a value
  • Get - To load/fetch an already saved value

Both Set and Get comes with a few different implementations for different datatypes.

  • GetInt/SetInt - For whole numbers (1, 4324, 212, etc)
  • GetFloat/SetFloat - For floating point numbers (0.0f, 1.43f, etc)
  • GetString/SetString - For strings/text (“Hello”, “Unity”, “Zaros”, etc)

To set an int, you would write like this

int myInt = 5;
PlayerPrefs.SetInt("Score", myInt);

To then fetch the int you have just saved, you would write like this

int fetchedInt = PlayerPrefs.GetInt("Score");

As you can see, I used the same string (“Score”) in both my Set and Get call. This string could be anything, and is used by PlayerPrefs to identify which value you would like to fetch.

using UnityEngine;

public class PlayerPrefTester : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            int scoreValue = 1;
            PlayerPrefs.SetInt("Score", scoreValue);
            Debug.Log("Set Score to=" + scoreValue);
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            int fetchedInt = PlayerPrefs.GetInt("Score");
            Debug.Log("Fetched 'Score', got value=" + fetchedInt);
        }
    }
}

Here is some code you can play around with. Paste this inside a new C# script, name it “PlayerPrefTester”, attach it to a game object, and press the Up Arrow to save, and press the Down Arrow to fetch/get.

Good luck!

1 Like

Well it’s really unclear how you’re filling your coins variable. I’m just guessing that maybe you need to make that a public static variable or put it on the script that collects your coins. You have Coins && coins. You wouldn’t need both, I don’t think.

Make Coins a public static variable and then run the code like this:

public void Save()
{
    PlayerPrefs.SetInt(coins_key, ResourcesCastle.Coins); // 1
    PlayerPrefs.Save(); // 2
}

There’s no known use for coins now.

1 Like