Countdown script problem...plus zoom in/out...

I solved the code: (^_^).b

var Num : int = 5;

function Start () {
    Readyfunc();
}

function Update() {
    if (Num > 0 )
    {
    CountDown();
    }
    else if (Num == 0)
    {
    CountDown();
    }
}

function CountDown() {
if (guiText.fontSize > 0)
    {
        guiText.fontSize = guiText.fontSize - 1;
        return;
    }
    else if (guiText.fontSize == 0)
    {
        guiText.fontSize = 98;
        Num = Num - 1;
        guiText.text = ""+ Num;
    }

    if (Num == 0)
    {
    guiText.fontSize = 98;
    guiText.text = "GO!";
    ready = true;
    Readyfunc(); //Unpause
    }
}

The problem is that while you think that this is a timer it's not. Update gets called everyframe - that is if someone runs your app with a low fps will have to wait more opposed to those who run it at an extremely high FPS might not even see the countdown it's so fast. I understand that you can't use sg. like speed and a multiplication by Time.deltaTime to shrink the text by the right amount since fontSize is an integer.

The way to go would be using yield. Yield WaitForSeconds what you should be interested in.

The part `while(true)` kills your app prolly. The frame it enters that loop it never gets out as 'true' is always true so it's an infinite loop. Breaking out is possible though that's not what you should do.

Again just use `yield WaitForSeconds()` with the number passed in `0.20` if you want it to wait `0.20` seconds before executing `Readyfunc()`.

Ohh also very important that as the doc says: "Note that you can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can."

But that's easy "just put everything you have"(just a bit of coding:) now in your above-written Update function into a dummy function that just gets called in Update and there you can do the yielding.


So based on your updated code I'll show you what I'd write: (read additional notes below)

var Num : int = 5;

var shrinkInterval : float = .01;

var maxSize : int = 98;

private var isSecondsTimerCoroutineRunning : boolean = false;

function Start () {
    StartCoroutine(textzoomout(shrinkInterval));

    guiText.fontSize = maxSize;

    guiText.text = Num.ToString(); // setting the starting text from code
}

function Update () {
    // don't want to start one every frame
    //StartCoroutine(textzoomout(shrinkInterval));

    if(Num > 0 && !isSecondsTimerCoroutineRunning)
    {
        isSecondsTimerCoroutineRunning = true;
        guiText.fontSize = maxSize;
        StartCoroutine(SecondsTimer(1));
    }
    else if (Num == 0)
    {
        guiText.fontSize = maxSize;
        guiText.text = "GO!";
        ready = true;
        Readyfunc(); //Unpause
        StopCoroutine("textzoomout");
        // instead of StopCoroutine(textzoomout(0.01));
    }
}

function SecondsTimer(waitTime : float) {
    yield WaitForSeconds(waitTime);
    Num = Num - 1;
    guiText.text = Num.ToString(); // changed - see below
    // the corouinte will return so it's finished
    isSecondsTimerCoroutineRunning = false;
    return;
}

function textzoomout (waitTime : float) {
    while(true)
    {
        yield WaitForSeconds(waitTime);
        guiText.fontSize = guiText.fontSize - 1;
        //return;
    }
} 

As you can see I also replaced you magicnumbers with public variables for the sake of easier testing. At first i wanted to say that it's no point adding a "" to Num but I realised that it was your way of making the compiler implicitly convert Num to a string but as you can see there are more proper ways to convert an integer to a string :).

More important stuff:

There was an error with StopCoroutine - corrected according to the doc which simply doesn't mention version such as yours.

Removed starting `textzoomout` every update frame which caused a too fast shrinking as multiple instances of the coroutine were being called all of them decreasing the `fontSize`. Also removed `return` as the goal of this coroutine is to shrink the `fontSize` every time it gets called so we let it run instead of starting more of it.

SecondsTimer would also be started multiple times in the same second so I added a variable checking if it's already running or not.

So now `textzoomout` is called only once from the `Start` function and gets the `fontSize` reset every second.

The code should work for your actual case and shouldn't be too hard too change and make it more errorproof with just a little scripting effort from here.

Maybe a problem could be that if the FPS is very low aside from the crappy gameplay experience the timer would take more time to count down to zero. People with higher FPS wouldn't experience any problems as the coroutines are executed at given steps. Though I don't you should worry about it but if you do then look into `Time.deltaTime` as I might have said earlier.