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!