- Home /
While loop not executing fully
This may be a rather stupid problem, but I can't figure out this while loop for the life of me. Basically, what it does is generate a random number, and then based on that number, it sends out a prompt for the player to type in. Once the player hits that prompt, it generates a new random number. For some reason, it generates the first random number, and then does not change the first prompt. I don't know whether it just generates the same number or if the prompt isn't working, but I'd really appreciate your help with this. Thanks in advance!
void Start()
{
StartCoroutine(waiter());
theKey = Random.Range(1, 6);
IsKeysEnabled = true;
//Switch to see which key they must press
if (IsKeysEnabled)
{
int x = 0;
while (x < 10)
{
if (theKey == 1)
{
GetComponent<SpriteRenderer>().sprite = w1;
if (Input.GetKeyDown("w"))
{
theKey = Random.Range(1, 6);
break;
}
}
else if (theKey == 2)
{
GetComponent<SpriteRenderer>().sprite = a;
bool press2 = Input.GetKeyDown("a");
//Success press
if (press2)
{
theKey = Random.Range(1, 6);
break;
}
}
else if (theKey == 3)
{
GetComponent<SpriteRenderer>().sprite = s;
bool press3 = Input.GetKeyDown("s");
//Success press
if (press3)
{
theKey = Random.Range(1, 6);
break;
}
}
else if (theKey == 4)
{
GetComponent<SpriteRenderer>().sprite = d;
bool press4 = Input.GetKeyDown("d");
//Success press
if (press4)
{
theKey = Random.Range(1, 6);
break;
}
}
else if (theKey == 5)
{
GetComponent<SpriteRenderer>().sprite = o1;
bool press5 = Input.GetKeyDown("o");
//Success press
if (press5)
{
theKey = Random.Range(1, 6);
break;
}
}
x++;
}
}
}
Answer by GamitusLabs · Oct 25, 2018 at 05:17 AM
it doesn't look like you don't actually need a while loop here, a simple if should work
all your work is in the start function... set stuff up in start and put your work in update
it looks like you want the correct key 10 times, so put the x++ in the success check
look into the basic tutorials for Unity and game programming in general... https://unity3d.com/learn
Hi! Thanks for commenting back! Exactly what "stuff" should I split between start and update?
Hello there!, I think everything from the "if" line can be put into an Update function. (Line 9). Update is executed constantly, while "Start" function is only executed once, so it's good for setting up the default value of variables and objects.
So, if i put just the code from the "if" line forward into update, the code runs repeatedly, which is another problem I run into. The code not breaking and stopping at 10 is one of the bigger problems I have with this. Also, if I set up the default value in Start or anywhere besides Update, the loop only runs once.
Your answer
![](https://koobas.hobune.stream/wayback/20220612180921im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there something wrong with my 'Jump' input axis? 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How To Make UI Button 2 Appear Three Seconds After UI Button 1 is pressed 1 Answer
How do I make a key activate only when another key is pressed 2 Answers
Getting (Input) values before running rest of script. 0 Answers