• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 25, 2018 at 07:51 PM by hexagonius for the following reason:

https://unity3d.com/de/learn/tutorials/s/scripting

avatar image
0
Question by Nolukdi · Oct 25, 2018 at 07:39 PM · updateloopstartwhile-looploops

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script and Start function of script are running, but Update function is not? 0 Answers

Question about the "WhileLoop" tutorial 1 Answer

Objects Instantiated in a Loop Problem 1 Answer

Update scene every 5 minutes 2 Answers

variables based on array.length not updating in JS 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges