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?
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.