- Home /
Question by
Jarrod.F · Nov 08, 2013 at 11:44 PM ·
timer-script
How do I make time restart when I hit Zero? C#
using UnityEngine;
using System.Collections;
public class PlayerTime : MonoBehaviour
{
private float startTime = 5;
private float restSeconds = 0;
private int roundedRestSeconds = 0;
private float displaySeconds = 0;
private float displayMinutes = 0;
public int CountDownSeconds = 120;
private float Timeleft = 0;
public Transform spawnPoin;
public GameObject player;
string timetext = "";
void Start ()
{
startTime = Time.deltaTime;
}
void ResetTimer ()
{
Timeleft = Time.time-startTime;
restSeconds = CountDownSeconds-(Timeleft);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = (roundedRestSeconds / 60)%60;
timetext = (displayMinutes.ToString() + ":");
if (displaySeconds > 9)
{
timetext = timetext + displaySeconds.ToString();
}
else
{
timetext = timetext + "0" + displaySeconds.ToString();
}
}
void OnGUI () {
startTime = 0;
Timeleft = Time.time-startTime;
restSeconds = CountDownSeconds-(Timeleft);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = (roundedRestSeconds / 60)%60;
timetext = (displayMinutes.ToString() + ":");
if (displaySeconds > 9)
{
timetext = timetext + displaySeconds.ToString();
}
else
{
timetext = timetext + "0" + displaySeconds.ToString();
}
if (displaySeconds<= 0)
{
player.transform.position = spawnPoin.position;
ResetTimer();
}
GUI.Label(new Rect(10, 10, 200, 40), timetext);
}
void ResetTime()
{
if(roundedRestSeconds == 0)
{
ResetTimer();
}
}
}
Comment
for including some code, but as it's uncommented and you have written no text in your question, its still pretty impossible to help you. What is this code meant to do? $$anonymous$$ost of it looks like it could be replaced with a single String.Format()
There's no variable in your code called "time" so what do you want to restart? startTime? Timeleft? Time.time?
And when you hit Zero? The zero key on the keyboard? Or when some counter hits zero? What counter?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make FPS trailer? 1 Answer
How to have animation play correctly 1 Answer
GameObject' does not contain a definition for `Find' 1 Answer