Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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: 

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

Loop animation 1 Answer

Script initialization when instancing a game object 0 Answers

Is it possible to call a method once per frame inside of another method? 2 Answers

Loop Logic - Update() and Quarternion.Slerp 1 Answer

variable update for player looping and keeps adding to level 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges