Hi I am writing my Pong game and there is a 3D text “Start Game”, once player click on it I want to stop the whole game and run “text animation”:
Get ready in 5…
Get ready in 4…
Get ready in 3…
.
.
.
Now in my javascript I wrote this:
function wait(n: float) {
//wait for n seconds
var t1: float;
t1 = Time.time;
while ((Time.time - t1) < n ) {
//waiting
}
}
And in the event:
function OnMouseOver () {
if(Input.GetMouseButton(0)) {
//stop the game
Time.timeScale = 0;
//play GET REDY animation before start
GetComponent(TextMesh).text = "Get ready in 5....";
wait(1);
GetComponent(TextMesh).text = "Get ready in 4....";
wait(1);
GetComponent(TextMesh).text = "Get ready in 3....";
wait(1);
GetComponent(TextMesh).text = "Get ready in 2....";
wait(1);
GetComponent(TextMesh).text = "Get ready in 1....";
wait(1);
//hide the text
renderer.enabled = false;
//reset ball
ball.GetComponent(BallBehaviour).Reset();
//start the game
Time.timeScale = 1;
}
}
But this solution leads Unity to actually freeze and I need to kill Unity form proccesses.
I’ve been experimenting with: yield WaitForSeconds(), but that doesn’t work either for my problem.
Any idea how to solve it?