- Home /
How do i get my timer to reset?
I have game with multiple levels and on the last one you can roll into a box that loads the start of the game back up. The problem is my timer and coin counter do not reset when you do this. I need some sort of on trigger reset that resets the game time and coin count.
Timer script c#
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Timer : MonoBehaviour {
public Text counterText;
public float seconds, minutes;
public static int coinCount = 0;
// Use this for initialization
void Start ()
{
counterText = GetComponent<Text> () as Text;
}
// Update is called once per frame
void Update ()
{
minutes = (int)(Time.time/60f);
seconds = (int)(Time.time % 60f);
counterText.text = minutes.ToString ("00") + ":" + seconds.ToString ("00") + (" Coins: ") + coinCount;
}
}
End Level script c#
using UnityEngine; using System.Collections;
public class EndLevel10 : MonoBehaviour {
void OnTriggerEnter () { Application.LoadLevel("MainMenu"); } }
Just use GetComponent to get the time/coin count in your EndLevel10 and set them to 0 when you enter the trigger... it'll reset the score and time AND will send you back to $$anonymous$$ain$$anonymous$$enu
Ok I did the get component and called the variable through the timer class, had to change variables to static, but now I am getting this error CS0029: Cannot implicitly convert type int to string. It is on line 19 of the EndFinalLvl script. I've looked for answeres regarding the error but could not find anything that pertains to my situation. Sorry it has been over a week. I've been swamped with graduation and college stuff.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EndFinalLvl : $$anonymous$$onoBehaviour {
void Start ()
{
Timer.counterText = GetComponent<Text> () as Text;
}
// Update is called once per frame
void OnTriggerEnter ()
{
Application.LoadLevel ("$$anonymous$$ain$$anonymous$$enu");
Timer.$$anonymous$$utes = (int)(Time.time/60f);
Timer.seconds = (int)(Time.time % 60f);
Timer.counterText.text = 0; //this is the line with the error
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : $$anonymous$$onoBehaviour {
public static Text counterText;
public static float seconds, $$anonymous$$utes;
public static int coinCount = 0;
// Use this for initialization
void Start ()
{
counterText = GetComponent<Text> () as Text;
}
// Update is called once per frame
void Update ()
{
$$anonymous$$utes = (int)(Time.time/60f);
seconds = (int)(Time.time % 60f);
counterText.text = $$anonymous$$utes.ToString ("00") + ":" + seconds.ToString ("00") + (" Coins: ") + coinCount;
}
}
Your answer
Follow this Question
Related Questions
PUN (Photon Unity Networking) Players Move Other Players 1 Answer
Assigning value to variable from JS script via C# script 1 Answer
Editing UIElements Label through code and alignment 1 Answer
How to disable a script on a bunch of instantiated objects with tag ? 1 Answer
urp custom shader? 0 Answers