x


[Closed] WaitForSeconds isn't working

I'm running this coroutine:

IEnumerator RandomDisplay () {
HealthPointsDisplay = Random.Range((HealthPoints - 150), (HealthPoints + 151));
FatiguePointsDisplay = Random.Range((FatiguePoints - 150), (FatiguePoints + 151));
SyncPointsDisplay = Random.Range((SyncPoints - 50), (SyncPoints + 51));
yield return new WaitForSeconds(1.0F);
}

Under Update() in an attempt to always give the player an incorrect display of several stats.

But, as it stands, running that changes the value so quickly that it hurts one's eyes. WaitForSeconds isn't doing anything to slow it down.

What am I doing wrong?

more ▼

asked May 20 '12 at 06:47 PM

burnpsy gravatar image

burnpsy
149 10 18 20

Are you calling RandomDisplay from Update? If so, WFS is working..but since new functions are continually called you will have X amount of WFS calls happening(which defeats the purpose). You can wrap the Update call with a boolean, then once the WFS has finished executing you can flip the bool back so the function will be called again.

May 20 '12 at 06:51 PM hijinxbassist

WaitForSeconds can only pause coroutines - the caller code will not even notice the pause, because StartCoroutine returns almost immediately (the coroutine is automatically resumed each frame, and runs until it finds an yield instruction).

May 20 '12 at 07:40 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

The question has been closed Dec 15 '12 at 08:56 AM by burnpsy for the following reason:

The question is answered, right answer was accepted


2 answers: sort oldest

The waitForSeconds is actually useless at this point. There is nothing after the yield statement. Coroutines run kind of parallel to your other code (not really parallel but almost ;) ).

When you start a coroutine a new coroutine object is created which is handled by Unity's coroutine scheduler in the background. You can't halt or delay Update. Update will be called every frame. Whenever you use StartCoroutine you create a new independent instance of thie coroutine.

You propably want something like that:

IEnumerator RandomDisplay ()
{
    while (true)
    {
        HealthPointsDisplay = Random.Range((HealthPoints - 150), (HealthPoints + 151));
        FatiguePointsDisplay = Random.Range((FatiguePoints - 150), (FatiguePoints + 151));
        SyncPointsDisplay = Random.Range((SyncPoints - 50), (SyncPoints + 51));
        yield return new WaitForSeconds(1.0F);
    }
}

void Start()
{
    StartCoroutine(RandomDisplay());
}

This will start one coroutine which will run forever and that changes your variables once a second.

more ▼

answered May 20 '12 at 07:18 PM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

That would do it. Thanks.

May 20 '12 at 07:53 PM burnpsy
(comments are locked)
10|3000 characters needed characters left
more ▼

answered May 20 '12 at 07:06 PM

Beriss gravatar image

Beriss
86 1 2

(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4171
x170

asked: May 20 '12 at 06:47 PM

Seen: 858 times

Last Updated: May 20 '12 at 07:53 PM