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++;
            }
        }
    }
  1. it doesn’t look like you don’t actually need a while loop here, a simple if should work
  2. all your work is in the start function… set stuff up in start and put your work in update
  3. it looks like you want the correct key 10 times, so put the x++ in the success check
  4. look into the basic tutorials for Unity and game programming in general… Learn Game Development Without Coding Experience | Unity