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 /
avatar image
0
Question by Nolukdi · Oct 25, 2018 at 05:04 AM · inputloopif-statements

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++;
             }
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by GamitusLabs · Oct 25, 2018 at 05:17 AM

  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... https://unity3d.com/learn

Comment
Add comment · Show 4 · Share
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
avatar image Nolukdi · Oct 25, 2018 at 07:23 PM 0
Share

Hi! Thanks for commenting back! Exactly what "stuff" should I split between start and update?

avatar image GrimoruCoffee Nolukdi · Oct 25, 2018 at 07:34 PM 0
Share

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.

avatar image Nolukdi GrimoruCoffee · Oct 25, 2018 at 07:42 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 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 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

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


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