- Home /
Previous UI text sticks around even after reloading scene using SceneManager.LoadScene(scene.name);
Hi all, newbie here. I'm making a simple Unity 5.6 game that has an elapsed time and a best time. Once the game is over, a blue "game over" screen animates in that lets people re-try the game. However, I running into an issue I can't resolve:
The initial elapsed time and initial best score are "burned" into the UI and don't clear after even after I reload the scene using SceneManager.LoadScene(scene.name);
See screenshots and code below.
I don't have any static variables for the timers. Is it an issue with the animator? I've spent a long time on this and can't figure it out. Thanks for any help in advance.
BallContactScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BallContactScript : MonoBehaviour {
public Text timerLabel;
public Text bestTimeLabel;
private int bestTime;
private float time;
private bool timerRunning = true;
private int minutes;
private int seconds;
private int milliseconds;
// Use this for initialization
void Start () {
Debug.Log ("time: " + time);
Debug.Log ("bestTime: " + bestTime);
Debug.Log ("PlayerPrefs.GetInt (bestTime): " + PlayerPrefs.GetInt ("bestTime", bestTime));
timerLabel.text = "Time: 0";
bestTime = PlayerPrefs.GetInt ("bestTime", bestTime);
bestTimeLabel.text = "Best: " + bestTime;
}
// Update is called once per frame
void Update () {
// Current Time
if (timerRunning) {
time += Time.deltaTime;
minutes = (int)time / 60;
seconds = (int)time % 60;
milliseconds = (int)(time * 10);
milliseconds = milliseconds % 10;
}
timerLabel.text = "Time: " + (minutes * 60 + seconds).ToString("0") + "." + milliseconds.ToString("0");
// Best time
if ((int)time > bestTime){
bestTime = (int)time;
bestTimeLabel.text = "Best: " + (int)time;
PlayerPrefs.SetInt ("bestTime", bestTime);
}
}
void SetTimerText () {
}
void OnTriggerExit(Collider other) {
if (other.tag == "Ball") {
timerRunning = false;
Debug.Log ("Lost contact");
Debug.Log ("bestTime: " + bestTime);
Debug.Log ("PlayerPrefs.GetInt (bestTime): " + PlayerPrefs.GetInt ("bestTime", bestTime));
// Tell GameManager lost contact with ball
GameManager.instance.PlayerLostContactWithBall ();
}
}
}
GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public static GameManager instance = null;
Animator anim;
private bool playerActive = false;
private bool gameOver = false;
public bool PlayerActive {
get { return playerActive; }
}
public bool GameOver {
get { return gameOver; }
}
void Awake() {
if (instance == null) {
instance = this;
} else if (instance != this) {
Destroy (gameObject);
}
DontDestroyOnLoad (gameObject);
if (anim == null) {
anim = GameObject.Find("HUDCanvas").GetComponent<Animator>();
}
DontDestroyOnLoad (anim);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayerLostContactWithBall () {
gameOver = true;
anim.SetTrigger("DidClearLevel");
}
public void PlayerRestartedLevel () {
gameOver = false;
// reset animation
anim.Play("Empty",0,0f);
}
public void PlayerStartedLevel () {
playerActive = true;
}
}
PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
// [SerializeField] GameObject prefab;
public List<GameObject> prefabs;
// Use this for initialization
void Start () {
Debug.Log ("Screen width: "+Screen.width + " Screen height: " + Screen.height);
}
// Update is called once per frame
void Update () {
if (!GameManager.instance.GameOver) {
//Left mouse click
if (Input.GetMouseButtonDown(0)) {
// Convert mouse screen coordinates to world coordinates
Debug.Log ("Mouse position: "+Input.mousePosition);
var mousePos = Input.mousePosition;
mousePos.z = -35; // select distance = -35 units from the camera
var mousePosConverted = Camera.main.ScreenToWorldPoint(mousePos);
Debug.Log(Camera.main.ScreenToWorldPoint(mousePos));
// Load prefab cubes
GameObject fab = prefabs[Random.Range(0,prefabs.Count)];
Instantiate(fab, new Vector3(-mousePosConverted.x,15,0), Quaternion.identity);
}
}
}
}
RestartLevel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RestartLevel : MonoBehaviour {
// Reload current level
public void RestartGameLevel() {
Debug.Log ("Restart Level");
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
// Reset level clear animation
GameManager.instance.PlayerRestartedLevel ();
}
// // Use this for initialization
// void Start () {
// }
// // Update is called once per frame
// void Update () {
// }
}
First play through shoes the time just fine:
Subsequent play throughs show the time from the initial play (Time: 9.4) behind the current time.
Have you tried loading the scene LoadSceneAsync?
Never$$anonymous$$d, your issue is that you are setting them to DontDestroyOnLoad. The intention for this is so they DO RE$$anonymous$$AIN on a new scene.