• 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 shezab · Jan 16, 2015 at 04:46 PM · script error

Having problem with a basic guess the number script

using UnityEngine; using System.Collections;

public class NumberWizard : MonoBehaviour {

 int max;
 int min;
 int guess;

 // Use this for initialization
 void Start () {
     
     StartGame ();
 }
 
 void StartGame () {
 
     max = 1000;
     min = 1;
     guess = 500;
 
     
     
     print ("===========================================");
     print ("Welcome to Number Wizard");
     print ("Pick a Number in your head But don't tell me!");
     
     print ("The highest number you can pick is " + max);
     print ("The lowest number you can pick is " + min);
     
     print ("Is the number higher or lower then " + guess);
     print ("UP = higher, DOWN = lower, return = equal");
     
     max = max + 1;
     
     
     
 }
 
 
 
 // Update is called once per frame
 void Update () {
 
     if (Input.GetKeyDown(KeyCode.UpArrow)) 
     
     {
         
         min = guess;
         NextGuess ();
         
         
         
     } 
     
     else if (Input.GetKeyDown(KeyCode.DownArrow)) 
         
     {
                     
         max = guess;
         NextGuess ();
         
         
     } 
     
     else if (Input.GetKeyDown(KeyCode.Return)) 
         
     {
         print ("I won!");
         StartGame ();
     }
 

}

 void NextGuess () {
     
     guess = (max + min) / 2;
     print ("Higher or lower than " + guess);
     print ("UP = higher, DOWN = lower, return = equal");
     
     
 }
 

}

Above is the code ... What i really wanted to do is for it to guess the number ... but as soon as i start pressing up or down arrow like 6 - 7 times , the guess gets stuck in a single number as indicated by the image and result in console below ... dont understand this behavior at the moment ..... Can u plz help me out in this regard ... Thanks

alt text

screen shot 2015-01-16 at 9.38.18 pm.png (117.5 kB)
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
0

Answer by Narv · Jan 16, 2015 at 05:03 PM

I'm not sure I understand your issue because it seems to be correctly doing what you want... It is stuck on 15 in this case because you had max at 16 and min at 14 so your next guess would have put it at 15 min max 16... so the middle would be 15.5 but since you are using an int it is truncated. Tracing your logic from the output it seems to be the following:

since you're at 125, you clearly hit DOWN for lower at first.. so it went to 1 - 500 so picked 250... so hit DOWN again and it went to 1 - 250 so picked 125. hit DOWN again so 1 - 125 so picked 62.5 but since it's an int rounded to 63. Hit DOWN so 1 - 63 so picked 31.5, rounded to 32. hit DOWN again so 1 - 32 and picked 16. You hit DOWN again and it went 1 - 16 so it picked 8. then you hit UP so it went 8 - 16 and picked 12. you hit UP so it went 12 - 16 and picked 14. you hit UP so it went 14 - 16 and picked 15... now you hit UP again instead of telling that it picked right since it picked 14 15 and 16 for you. so you're lying to the poor computer.. but if you hit UP you got 15 - 16.. which would pick 15.5 and it rounded it to 15. and if you hit DOWN it would go 15 - 16 and repeat, and up would be 15-15 and stuck.. I'm not seeing an error.

What number did you intend because this is what you got from your code..

Comment
Add comment · Show 1 · 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 shezab · Jan 16, 2015 at 05:28 PM 0
Share

Thank you so much .... Guess its the logical explaination of the happening that i was unable to comprehend rather then the logic within the code itself .... Thanks a lot yet again ....

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

all my scripts stopped working after i put a enemy spawner script in 0 Answers

HostListReceived symbol resolve 1 Answer

error CS1525: Unexpected symbol 'rigidbody' 1 Answer

C# script works wrong ( GUI ) How to fix it ? 1 Answer

Please. Help me out :( 1 Answer


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