I am having a "problem" in a game that is being developed. When the character dies from a collision to the enemy, or trigger gameobjects, it displays a game over screen, which contains a "Try Again" button to restart the game, but the score collected from the previous game remains once the restart button is clicked.
How can I solve this problem? Thank you for your help!!
GameOver C# script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CS_GameOverMenu : MonoBehaviour
{
public void RestartButton()
{
SceneManager.LoadScene("Level 1");
Debug.Log("Game open");
}
public void MenuButton()
{
SceneManager.LoadScene("MainMenu");
Debug.Log("Main Menu open");
}
public void ExitButton()
{
Application.Quit();
Debug.Log("Game closed");
}
//public void QuitButton()
//{
// Application.Quit();
//Debug.Log("Game closed");
}
As you have a static field, this data will persist while the game is still running even after a reset. Inside of your Reset
function put code similar to this.
If you would rather not grab the object by name, use this snippet.
public void RestartButton()
{
ScoringSystem.theScore = 0;
SceneManager.LoadScene("Level 1");
Debug.Log("Game open");
}
You just need to set the score back to 0 when reloading the scene as the value you have is marked as static.