PlayerPrefs - Coins

Hello,
I’m trying to create a variable that contains all the coins collected in my game, how can I add the collected coins in a single match to the variable that contains all the coins?

This is my script:

using UnityEngine;

using System.Collections;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour {

    public Text scoreText;
    public Text hiScoreText;

    public Text coinsText;
    public Text hiCoinsText;

    public float scoreCount;
    public float hiScoreCount;

    public float coinsCount;
    public float hiCoinsCount;

    public float pointsPerSecond;

    public bool scoreIncreasing;

    void Start ()
    {
        if (PlayerPrefs.GetFloat ("HighScore") != null)
        {
            hiScoreCount = PlayerPrefs.GetFloat ("HighScore");
        }
            hiCoinsCount = PlayerPrefs.GetFloat ("HighCoins");
    }

    void Update ()

    {
        if (scoreIncreasing)
        {
            scoreCount += pointsPerSecond * Time.deltaTime;
        }

        //PlayerPrefs.SetFloat ("HighCoins", hiCoinsCount);

        if (scoreCount > hiScoreCount)
        {
            hiScoreCount = scoreCount;
            PlayerPrefs.SetFloat ("HighScore", hiScoreCount);
        }

        scoreText.text = "SCORE: " + Mathf.Round(scoreCount);
        hiScoreText.text = "HIGH SCORE: " + Mathf.Round (hiScoreCount);

        coinsText.text = "COINS: " + Mathf.Round (coinsCount);
        hiCoinsText.text = "TOTAL COINS: " + Mathf.Round (hiCoinsCount);
    }
}

This is the script for each single coin:

using UnityEngine;
using System.Collections;

public class PickUpCoins : MonoBehaviour {

    public int scoreToGive;

    private ScoreManager theScoreManager;

    void Start ()
    {
        theScoreManager = FindObjectOfType<ScoreManager>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
            theScoreManager.coinsCount += scoreToGive;
    }

}

Thanks in advance!

You’ve got the basics down, but you’re missing a little bit of organization and design to do what you want.

Take a look a the changes I’ve made here. It’s pretty straightforward, but if you don’t understand something or want to know why I did something, feel free to ask.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour {

    private const string HIGH_SCORE_KEY = "HighScore";
    private const string HIGH_COINS_KEY = "HighCoins";

    public Text scoreText;
    public Text hiScoreText;

    public Text coinsText;
    public Text hiCoinsText;

    public float pointsPerSecond;
    public bool scoreIncreasing;

    private float scoreCount;
    private float hiScoreCount;

    private int coinsCount;
    private int hiCoinsCount;

    private void Start() {
        if(PlayerPrefs.HasKey(HIGH_SCORE_KEY)) {
            hiScoreCount = PlayerPrefs.GetFloat(HIGH_SCORE_KEY);
        }
        if(PlayerPrefs.HasKey(HIGH_COINS_KEY)) {
            hiCoinsCount = PlayerPrefs.GetInt(HIGH_COINS_KEY);
        }
    }

    private void Update() {
        if(scoreIncreasing) {
            AddScore(pointsPerSecond * Time.deltaTime);
        }
    }

    public void AddScore(float score) {
        scoreCount += score;
        scoreText.text = "SCORE: " + Mathf.Round(scoreCount);

        if(scoreCount > hiScoreCount) {
            hiScoreCount = scoreCount;
            hiScoreText.text = "HIGH SCORE: " + Mathf.Round(hiScoreCount);
            PlayerPrefs.SetFloat(HIGH_SCORE_KEY, hiScoreCount);
        }
    }

    public void AddCoins(int coins) {
        coinsCount += coins;
        coinsText.text = "COINS: " + coinsCount;

        if(coinsCount > hiCoinsCount) {
            hiCoinsCount = coinsCount;
            hiCoinsText.text = "TOTAL COINS: " + hiCoinsCount;
            PlayerPrefs.SetInt(HIGH_COINS_KEY, hiCoinsCount);
        }
    }
}
using UnityEngine;
using System.Collections;

public class PickUpCoins : MonoBehaviour {

    public int scoreToGive;

    private ScoreManager theScoreManager;

    void Start() {
        theScoreManager = FindObjectOfType<ScoreManager>();
    }

    void OnTriggerEnter(Collider other) {
        if(other.gameObject.tag == "Player") {
            // this could just add a coin, and then the score manager would add score based on score per coin
            // or perhaps coins can have different values but they still count as 1 coin.
            theScoreManager.AddCoins(1);
            theScoreManager.AddScore(scoreToGive);
        }
    }
}
1 Like