- Home /
Game Over Screen Problem
Hello, I'm new to Unity and need some help. What I'm hoping to happen is for a box to show up when the timer reaches 0. For some reason it just wont happen. Here is my script.
var Timer : float = 60;
static var Level1TargetAmounts = 5;
static var gameOver : boolean = false;
function OnGUI ()
{
GUI.Label (Rect (10, 30, 100, 20), Timer.ToString());
GUI.Label (Rect (10, 50, 200, 20), "Amount Of Targets Left : " );
GUI.Label (Rect (160, 50, 100, 20), Level1TargetAmounts.ToString());
if(gameOver.active == true)
{
GUI.Box (Rect (0, 0, Screen.width, Screen.height),"");
}
}
function Update()
{
Timer -= Time.deltaTime;
if (Timer <= 0)
{
Timer = 0;
gameOver = true;
}
}
Any suggestions?
so, do you want the timer to be set to 0 after 60 seconds? because there is an easier way to do it if that is what you are trying to get at!!!!!!
What, WaitForSeconds(60);? Unfortunately, doing it that way does not allow the player to see how long there is left in the game.
Answer by syclamoth · Oct 02, 2011 at 05:51 AM
One line-
if(gameOver.active == true)
isn't really a thing. You should be using just
if(gameOver)
Answer by Grady · Oct 02, 2011 at 05:55 AM
I would have said do something like this:
function OnGUI ()
{
if(Time.time >= 60){
GUI.Label (Rect (10, 30, 100, 20), Timer.ToString());
GUI.Label (Rect (10, 50, 200, 20), "Amount Of Targets Left : " );
GUI.Label (Rect (160, 50, 100, 20), Level1TargetAmounts.ToString());
GUI.Box (Rect (0, 0, Screen.width, Screen.height),"");
}
}
That would make your GUI function code run after 60 seconds (1 minute).
I hope this is what you wanted, if not, the comment back!
-Grady
Presumably, the 'timer.tostring' bit is talking about a countdown to said 60 seconds, so I'm pretty sure you'd want to see that before the $$anonymous$$ute is up. Besides, this method only works once, and assumes you want to get in to the game immediately- no room for a main menu or any such thing. Using Time.time like this can cause unexpected behaviour if you want to be able to reset your timers.
Answer by lbalasubbaiahapps · Oct 02, 2011 at 08:15 AM
using UnityEngine; using System.Collections;
[ExecuteInEditMode] public class TimeCountTransition : MonoBehaviour { public GUIText timeShow; public int time; public Texture2D texture_bg; //after time over
//-----------------initalization of audio-----------------------------------------------------------------------------------------------------
public AudioClip ac_ButtonSound;
public AudioSource audioSource;
public GUIStyle myStyle;
void OnGUI()
{
GUI.Label(new Rect(420,50,100,50),"Time:",myStyle);
timeShow.text = time.ToString();
if(time==5)
{
GUI.Label(new Rect(100,100,450,50),"Your time will be closed with in five seconds",myStyle);
}
if(time==0)
{
GUI.DrawTexture(new Rect(0,0,600,450),texture_bg); //background texture
GUI.Label(new Rect(150,200,300,250),"Your time is over if you want to play again press Restart button",myStyle);
if(GUI.Button(new Rect(200,300,100,50),"Restart"))
{
audioSource.PlayOneShot(ac_ButtonSound);
Application.LoadLevel(Application.loadedLevel);
}
else if(GUI.Button(new Rect(350,300,100,50),"Quit"))
{
audioSource.PlayOneShot(ac_ButtonSound);
Application.LoadLevel("Start");
}
}
}
void Start ()
{
time=60;
InvokeRepeating("Subtract", float.Epsilon, 1);
}
void Subtract()
{
if(time > 0)
{
--time;
}
}
IEnumerator WaitTimeForScreen()
{
yield return new WaitForSeconds(1);
}
}
once tried this one.if you want to time increment you can use ++time it will time increment.