- Home /
              This question was 
             closed Oct 25, 2018 at 07:51 PM by 
             hexagonius for the following reason: 
              
            
 
            While loop issues in Update() vs. Start()
Okay! Here's the situation: In this script, unity is supposed to prompt the user with a button that they must press. I am trying to get the user to finish the game if they press the correct buttons ten times. Unfortunately, this piece of code is giving me MAJOR PROBLEMS! When inside the Update(), the code simply executes forever (presumably because the x resets to 0 every frame). However, when the x is declared in Start() or anywhere else, the loop only runs one time. If I move the loop to start it also only runs once. I would really appreciate some help on this. Thank you!
 public int x;
 
     // Use this for initialization
     void Start()
     {
         StartCoroutine(waiter());
         theKey = Random.Range(1, 6);
         IsKeysEnabled = false;
     }
 
     IEnumerator waiter()
     {
         yield return new WaitForSeconds(3);
         IsKeysEnabled = true;
     }
 
     // Update is called once per frame
     void Update()
     {
         x = 0;
         while(x < 1)
         {
             if (theKey == 1)
             {
                 GetComponent<SpriteRenderer>().sprite = w1;
 
                 if (Input.GetKeyDown("w"))
                 {
                     theKey = Random.Range(1, 6);
                     x++;
                 }
 
 
             }
 
             else if (theKey == 2)
             {
                 GetComponent<SpriteRenderer>().sprite = a;
 
                 //Success press
                 if (Input.GetKeyDown("a"))
                 {
                     theKey = Random.Range(1, 6);
                     x++;
                 }
 
             }
 
             else if (theKey == 3)
             {
                 GetComponent<SpriteRenderer>().sprite = s;
 
                 //Success press
                 if (Input.GetKeyDown("s"))
                 {
                     theKey = Random.Range(1, 6);
                     x++;
                 }
 
             }
 
             else if (theKey == 4)
             {
                 GetComponent<SpriteRenderer>().sprite = d;
 
                 //Success press
                 if (Input.GetKeyDown("d"))
                 {
                     theKey = Random.Range(1, 6);
                     x++;
                 }
             }
 
             else
             {
                 GetComponent<SpriteRenderer>().sprite = o1;
 
                 //Success press
                 if (Input.GetKeyDown("o"))
                 {
                     theKey = Random.Range(1, 6);
                     x++;
                 }
             }
 
             x++;
 
         }
 
     }
               Comment
              
 
               
               koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                