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 kearnsj2643 · Apr 08, 2019 at 06:00 PM · uitimerreset

Why is this Reset code not working?

On a game that was made from the website "Zulama" I have attempted to get the game to reset the timer UI Object. This timer UI Object should count up the number of seconds the player was in the game for and post them on the end screen. However, the problem is when the game is played again, the end screen will also have added the timer from the previous attempts as well. I have the following code on a persistent data object that has the timer on it with a reset code that should reset the timer back to 0 on start.


using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class PersistentData : MonoBehaviour { // public variables that can be set in the Inspector

 // private variables
 public int maxTime = 0;
 public Text timerUI;    
 private int timer;        // in seconds - how long to play


 // Use this for initialization
 void Start () 
 {
     timer = maxTime;
     // make this object persistent
     DontDestroyOnLoad(transform.gameObject);
     InvokeRepeating ("CountUp", 1, 1);
     Reset ();
 }
 
 // Update is called once per frame
 void Update () 
 {
     
 }
     
 void CountUp()
 {
     timer += 1;
 }

 public int GetTimer()
 {
     return timer;
 }

 void Reset()
 {
     timer = 0;
 }

}


then after either the player has lost or has won the game will take them to the specified end screen where the timer is called and the timer with the number of seconds is posted using one of these scripts.

If the player won

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class GetTimer2 : MonoBehaviour {

 public Text timerUI;
 private PersistentData persistentScript;

 // Use this for initialization
 void Start () 
 {
     persistentScript = GameObject.Find("PersistentObject").GetComponent<PersistentData>();
     gameObject.GetComponent<Text> ().text = "Time Complete : " + persistentScript.GetTimer ();
     timerUI.text = string.Format ("Time Completed {0:0}:{1:00}", (persistentScript.GetTimer ()) / 60, (persistentScript.GetTimer ()) % 60);
 }

 // Update is called once per frame
 void Update () 
 {
 }

}


Or if the player lost

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class GetTimer : MonoBehaviour {

 public Text timerUI;
 private PersistentData persistentScript;

 // Use this for initialization
 void Start () 
 {
     persistentScript = GameObject.Find("PersistentObject").GetComponent<PersistentData>();
     gameObject.GetComponent<Text> ().text = "Time Spent : " + persistentScript.GetTimer ();
     timerUI.text = string.Format ("Time Spent {0:0}:{1:00}", (persistentScript.GetTimer ()) / 60, (persistentScript.GetTimer ()) % 60);
 }
 
 // Update is called once per frame
 void Update () 
 {
 }

}


Any help or feedback on why the timer does not reset would be greatly appreciated.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by highpockets · Apr 09, 2019 at 08:54 AM

If it is persistent and you are only calling Reset() in the Start() method it will only be called once at the start of the first game I believe. Try making Reset() a public method and calling it at the end of your win or loss code on the GetTimer scripts:

 void Start () 
  {
      persistentScript = GameObject.Find("PersistentObject").GetComponent<PersistentData>();
      gameObject.GetComponent<Text> ().text = "Time Spent : " + persistentScript.GetTimer ();
      timerUI.text = string.Format ("Time Spent {0:0}:{1:00}", (persistentScript.GetTimer ()) / 60, (persistentScript.GetTimer ()) % 60);
 persistentScript.Reset();
  }
Comment
Add comment · Show 1 · Share
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 kearnsj2643 · Apr 09, 2019 at 03:43 PM 0
Share

Thank you @highpockets You were absolutely right, it now Resets appropriately.

avatar image
0

Answer by haizathaneefa · Apr 09, 2019 at 08:49 AM

I am going out on a limb here and say that you are calling your reset function on Start which only happen once. Call

 void Reset()
  {
      timer = 0;
  }

whenever the player won or lost. Refer here. It's not exactly what you want but it can give you an idea on what you have to do.

P/S = I may be wrong, just giving you my opinion on the matter.

Comment
Add comment · Share
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
0

Answer by zakkaiokenx10 · Apr 09, 2019 at 09:25 AM

What i want you to note, is that the start method only runs once the whole game.

A better way to achieve this is using time += time.deltatime in the update method; where the code runs every frame.

and by calling reset(); when you want to reset it.

I must say this though, for your case update text in the update method.

 timerUI.text = string.Format ("Time Spent {0:0}:{1:00}", (persistentScript.GetTimer ()) / 60, (persistentScript.GetTimer ()) % 60);

you'll have a better chance of seeing what that code does.

Comment
Add comment · Share
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

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

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timer question 1 Answer

Button animation (transition) reset 0 Answers

Time limit on my game with selection menu 0 Answers

Reset the countdown timer 2 Answers

Timer reset after elapsed time. 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