- Home /
Game over?
I have a timer and and when the time runs out I want a button to appear that says time is up, game over, and play again. Im not sure how to do this or where to put it. heres what i have for my code.
[code]
var isPaused:boolean = false;
var startTime:float;
var timeRemaining:float;
var percent:float;
var clockBG:Texture2D;
var clockFG:Texture2D;
var clockFGMaxWidth:float;
function Start()
{
guiText.material.color = Color.black;
startTime = 10;
clockFGMaxWidth = clockFG.width;
}
function Update () {
if(!isPaused)
{
DoCountdown();
}
}
function DoCountdown()
{
timeRemaining = startTime - Time.timeSinceLevelLoad;
percent = timeRemaining/startTime * 100;
if (timeRemaining <0)
{
timeRemaining = 0;
isPaused = true;
TimeIsUp();
}
Showtime();
Debug.Log(" time remaining = " + timeRemaining);
}
function PauseClock()
{
isPaused = true;
}
function UnPauseClock()
{
isPaused = false;
}
function Showtime()
{
var minutes:int;
var seconds:int;
var timeStr:String;
minutes = timeRemaining/60;
seconds = timeRemaining % 60;
timeStr = minutes.ToString() + ":";
timeStr += seconds.ToString("D2");
guiText.text = timeStr;
}
function TimeIsUp()
{
Debug.Log("Time Is Up");
}
function OnGUI()
{
var newBarWidth:float = (percent/100) * clockFGMaxWidth;
var gap:int = 20;
var isPastHalfway:boolean = percent<50;
var clockRect:Rect = Rect(0,0,128,128);
var rot:float = (percent/100) * 360;
var centerPoint:Vector2 = Vector2(64, 64);
var startMatrix:Matrix4x4 = GUI.matrix;
GUI.BeginGroup(new Rect (Screen.width - clockBG.width -gap, gap, clockBG.width, clockBG.height));
GUI.DrawTexture(Rect(0,0,clockBG.width, clockBG.height), clockBG);
GUI.BeginGroup(new Rect(5,6,newBarWidth,clockFG.height));
GUI.DrawTexture(Rect(0,0,clockFG.width, clockFG.height), clockFG);
GUI.EndGroup();
GUI.EndGroup();
}
[/code]
Answer by Fattie · Aug 24, 2012 at 07:59 PM
are you familiar with the Invoke() command ? (there's also InvokeRepeating() )
it makes things like this incredibly easy. Check it out.
No problem. It's really strange, it is the most common command you use everyday to make video games. But very many programmers new to Unity have never heard of it. it's just one of those things! Cheers
Your answer
Follow this Question
Related Questions
No esc when game over. 1 Answer
Display Time At Game Over Scene 1 Answer
Script for Car 2 Answers
Game over when time reached 1 Answer
Start timer with Input 2 Answers