- Home /
Make label disappear after 1 second?
Hi, so I'm making a car game for my university project and I'm trying to implement a countdown timer at the begging of the game which will countdown from 3 and tell the player to go.
I have it working as it should, the player and AI can't move until the timer reaches 0 but the problem is that I'm using labels to display "3", "2", "1" and "GO!" when it should be displayed but the labels don't disappear and just stack. I've tried a coroutine and using a boolean but neither worked. Here's the code from the OnGUI method:
using UnityEngine;
using System.Collections;
public class StartCountdownScript : MonoBehaviour
{
public float countdownTime = 7.0f;
public float resetPlayerTorque;
public float resetAITorque;
public GameObject[] aiCars;
public GameObject player;
public GUIStyle customGUIStyle;
public int labelWidth = 250;
public int labelHeight = 250;
public bool toggleLabel = false;
void OnGUI()
{
if (countdownTime <= 5)
{
GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "3", customGUIStyle);
}
if (countdownTime <= 3)
{
GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "2", customGUIStyle);
}
if (countdownTime <= 1)
{
GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "1", customGUIStyle);
}
if (countdownTime <= 0)
{
GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "GO!", customGUIStyle);
}
}
Can someone tell me how to go about displaying each label for just 1 second then disappearing? I'm sure I'm overlooking something so simple or have made a silly mistake.
All help is appreciated, Thanks ;)
Answer by Lo0NuhtiK · Mar 31, 2014 at 10:11 PM
They're stacking because 0
Ex :
float time = 7f ;
string content = "" ;
bool countDownDone = false ;
void Update()
{
//decrement time
time -= Time.deltaTime ;
}
void OnGUI()
{
CountDown() ;
}
void CountDown()
{
//early-out of this function if we've already done the countdown
if(countDownDone) return ;
int t = Mathf.FloorToInt(time) ;
switch(t)
{
case 4 :
content = "3" ;
break ;
case 3 :
content = "2" ;
break ;
case 2 :
content = "1" ;
break ;
case 1 :
content = "GO!!" ;
break ;
case 0 :
countDownDone = true ;
break ;
}
GUI.Label(--yourRect--, content, --yourStyle) ;
}
Deleted my answer since yours is more detailed. $$anonymous$$ine worked, but it depends on how you decrement the timer value.
Damn it lmao .. I just posted a comment on your answer.. ninja'd me again
---Yeah, I guess it would depend on that. But assu$$anonymous$$g that they decrease T by deltaTime in Update() and don't round it to an int like I did, then (==) would be lucky to ever even hit "1" because T could be 1.023 this update, and then be 0.283899 the next one, etc.
I called InvokeRepeating() on a method at Start() at a 1 second interval, method simply decremented the timer. $$anonymous$$aybe should have included that. :)
Thanks for the detailed answer. I completely overlooked that the if statement is always true.
Thanks, I'll give this a shot later on today.
So I put this together, in place of all my if statements and it works like a charm.
Thank you very much.
Answer by oktemre · Mar 31, 2014 at 10:14 PM
Where do you decrease your countdownTime variable? I suggest adding a line to the update function as below;
countdownTime -= Time.deltaTime;
also don't make seperate if statements and use else if.