Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mhsu · May 14, 2017 at 10:49 PM · uitextscenescene-loading

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:

alt text

Subsequent play throughs show the time from the initial play (Time: 9.4) behind the current time.

alt text

screen-shot-2017-05-12-at-111247-pm.png (36.2 kB)
screen-shot-2017-05-12-at-111301-pm.png (37.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 14, 2017 at 11:36 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UI Scene ending with Text 1 Answer

Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers

How do I switch scenes by obtain a children ui canvas text 0 Answers

I want to bring the text written in Input field in the gameplay scene to the white place in the wrong scene. 0 Answers

UI button loading wrong scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges