Good Morning!
I’m trying to do a simple thing, catch a text element from my UI and store the total points of the player.
So i created a script called Score:
public Text scoreText;
private int playerScore = 0;
private int totalScore = 0;
void Start()
{
scoreText.GetComponent<Text>();
}
void Update()
{
totalScore = playerScore + Feed.hit;
scoreText.text = totalScore.ToString("0");
}
The Feed.hit is from the script Feed, this add a punctuation when an object hit another:
public static int hit = 0;
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Doe")
{
hit+=5;
Destroy(other.gameObject);
}
if(other.gameObject.tag == "Stag")
{
hit+=10;
Destroy(other.gameObject);
}
if(other.gameObject.tag == "Moose")
{
hit+=15;
Destroy(other.gameObject);
}
}
On my GameManager i call the Restart if the game is over and on my method “RestartGame()”
i also do the reset of score:
public GameObject gameOver;
public static GameManager instance;
public static bool GameIsPause = false;
private Score scoreManager;
void Start()
{
instance = this;
scoreManager = FindObjectOfType<Score>();
}
public void ShowGameOver()
{
gameOver.SetActive(true);
Time.timeScale = 0f;
GameIsPause = true;
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Time.timeScale = 1f;
GameIsPause = false;
scoreManager.scoreText.text = "0";
}
But it never reset my score.
I tried to do this on my score too:
public Text scoreText;
private int playerScore = 0;
private int totalScore = 0;
void Start()
{
scoreText.text = PlayerPrefs.GetInt("TotalScore", 0).ToString();
}
void Update()
{
totalScore = playerScore + Feed.hit;
scoreText.text = totalScore.ToString("0");
PlayerPrefs.SetInt("TotalScore", totalScore);
}
public void DeleteScore()
{
PlayerPrefs.DeleteAll();
totalScore = Feed.hit * 0;
scoreText.text = totalScore.ToString("0");
}
Even when starting the game set the score to 0, but nothing.
Can someone help me?