Question by
Sokis · Aug 19, 2017 at 08:49 AM ·
randomcoroutinetimerif-statementsrandom.range
How do I generate a random number in regular time intervals?
What I basically want to do is generate a random number between 0 and 4 (4 is exclusive) and then display that number in the debug log. I have a boolean variable which controls the generator, so that it occurs once every 7 seconds. But for some reason, it occurs every frame. How do I make the random number be generated only once every 7 seconds?
public class MotherBotFightController : MonoBehaviour {
int IndexNumber;
public static bool GenerateFormation = false;
void Update () {
if (GenerateFormation == false) {
StartCoroutine (FormationReset ());
} else {
IndexNumber = Random.Range (0, 4);
Debug.Log(IndexNumber);
GenerateFormation = false;
}
}
IEnumerator FormationReset(){
yield return new WaitForSeconds (7);
GenerateFormation = true;
}
}
Comment
Your answer
Follow this Question
Related Questions
Randomly choosing between 3 options 1 Answer
Coroutine not working as expected 1 Answer
How to generate a random color? 5 Answers
Need help with specifics in Random.Range 1 Answer
Start and Stop with key input? 2 Answers