- Home /
3, 2, 1 GO!
So I'm trying to get a 3, 2, 1 GO! thing going on but It just doesn't seem to be working, no errors just not working, any help?
private bool showCountdown;
private string countdown = "";
void Start () {
instance = this;
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
gameOverText.enabled = false;
}
void Update () {
if(Input.GetButtonDown("Jump")){
GetReady();
GameEventManager.TriggerGameStart();
}
if (Input.touchCount > 0){
GameEventManager.TriggerGameStart();
}
}
IEnumerator GetReady () {
showCountdown = true;
countdown = "3";
yield return new WaitForSeconds (1.5f);
countdown = "2";
yield return new WaitForSeconds (1.5f);
countdown = "1";
yield return new WaitForSeconds (1.5f);
countdown = "GO!";
yield return new WaitForSeconds (1.5f);
showCountdown = false;
countdown = "";
}
void OnGUI(){
if (showCountdown){
GUI.color = Color.red;
GUI.Box (new Rect (Screen.width / 2 - 100, Screen.width / 2, 200, 175), "GET READY");
GUI.color = Color.white;
GUI.Box (new Rect (Screen.width / 2 - 90, Screen.width / 2, 180, 140), countdown);
}
}
Answer by falconer · Feb 20, 2013 at 07:53 PM
you could use this code instead,
var guiCountDown : GUIText;
var countMax : int;
private var countDown : int;
function Start(){
guiCountDown.enabled=true;
GameStart();
}
function Update () {
}
function GameStart(){
var car = gameObject.Find("NAME_OF_CAR");
var drivingScript = car.GetComponent("NAME_OF_DRIVING_SCRIPT");
drivingScript.enabled=false;
for (countDown = countMax; countDown>0;countDown--){
guiCountDown.text = countDown.ToString();
yield WaitForSeconds(1);
}
guiCountDown.enabled=false;
drivingScript.enabled=true;
}
create a GUIText and add it to the script in the inspector section.make sure it is positioned so that it can be seen on the screen where the car runs. Set countdownmax to 3 if you want the count to start from 3..
completely rewrote my code using a guitext object, looks nothing at all like this code but the part about guitext helped me realize how to fix it
Answer by robertbu · Feb 20, 2013 at 04:46 AM
To run a coroutine you need to call StartCoroutine() rather than call the routine directly. For example:
StartCoroutine(GetReady());
O$$anonymous$$ I've changed that but it's still not working, it kinda just skips the countdown and starts the game.
Well it's not on the runner it's on a gameobject that handles the GUI and general stuff.
ok, I think you should disable the driving script until the countdown is completed.
The 'driving' script IS disabled until the game 'starts', the problem is the countdown isn't even happening at all.
Answer by Gabriel Quijada · Feb 23, 2013 at 01:00 AM
In fact you have to call the coroutine whit
StartCoroutine("GetReady");
that should works
Uhm, it's better to call it like robertbu said. It's faster, less error prone and allows more than one parameters.
Your answer
Follow this Question
Related Questions
Coin Counting Problem 0 Answers
Countdown To Start Game 2 Answers
Countdown remove gui.box 2 Answers
Why is yield not working for me? 2 Answers
Problems in c# countdown timer 1 Answer