• 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
Question by JacobHockey13 · Jul 23, 2014 at 10:53 PM · guibooleaniffalseguibutton

Why is Unity registering my false boolean as true?

Right now I am designing an instructions scene, and I cannot determine why Unity is registering my false boolean as true. Most of this scripting is just for context and you can totally ignore my first GUI.Label text. I have a boolean "giveTenPoints" which simply loads a GUI.Label that says "+10". But the catch is I only want to have it appear after the user has pressed my GUI.Button. To do this, I have a "giveTenPoints" boolean that becomes true after the user hits the button. But for some reason it is true from the start of the scene and ten points shows up immediately, not waiting for the user to hit the button. My other boolean "hasBeenPressed1" works perfectly fine and only becomes true after pressing the button so that it effectively deletes it. Thank you for the help.

     #pragma strict
     
     var myStyle : GUIStyle;
     var scrollPosition : Vector2 = Vector2.zero;
     private var howFarRight : int = Screen.width;
     private var howFarDown : int = Screen.height;
     private var difference : int = (Screen.width - 153);
     private var centering : int;
     var practiceOrangeDot : Texture; 
     private var orangeCenter : int;
     private var hasBeenPressed1 : boolean = false; 
     private var giveTenPoints : boolean = false;
      
      
      function Start () {
      centering = difference;
      centering /= 2;
      orangeCenter = centering;
      orangeCenter += 40;
      }
     
      function OnGUI ()
      {
        scrollPosition = GUI.BeginScrollView (Rect (0,0,howFarRight,howFarDown), scrollPosition, Rect (0, 0, 0, 1250));
      GUI.Label (Rect (centering, 0, 130, 1300), "Dots or Die is a simple game with a simple premise: Tap all the orange dots, and leave the other dots alone. If you do this, you will be handsomely rewarded. Just for touching an orange dot, you will receive ten points. A whole ten points! Here, let’s practice, shall we?\n\n\n\n\n\n\nBrilliant! (assuming you hit the dot, otherwise I’m congratulating you for being disobedient and doing absolutely nothing). Now, hit this red dot.\n\n\n\n\n\n\nSo if you hit a red dot (or any dot that isn’t orange for that matter), you are now out of twenty hard-earned points. I know you’re thinking, that’s ridiculous, if I do my job, I only garner ten points, but then I make a mistake and twenty are deducted. Well, in short, yes… but think about this. Why are there five weekdays and only two days in the weekend? That’s an even worse ratio. Maybe this ostensibly simple game is actually a beautiful allegory acutely highlighting life’s injustices and iniquities? I’m not claiming it is, but there’s certainly room for discussion on it (and room for it on the description in the app store). That’s enough for now though, so I’ll let you play the game and decide. ", myStyle);
      
      if (!hasBeenPressed1) {
      if (GUI.Button(Rect(orangeCenter,275,50,50),practiceOrangeDot,myStyle))
                 hasBeenPressed1 = true;
                 giveTenPoints = true;
       }
       if (giveTenPoints == true) {
       GUI.Label (Rect (orangeCenter, 285, 50, 50), "+10", myStyle);
       } 
        GUI.EndScrollView ();
      }
      
     
             
Comment

People who like this

0 Show 0
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
Best Answer

Answer by robertbu · Jul 23, 2014 at 11:03 PM

You have no brackets ({}) after your 'if(GUI.Button())' line. That means the 'if()' statement will only be applied to the next line. If you want multiple lines inside an 'if()' statement, you need brackets like you use for 'if (!hasBeenPressed1)'.

Comment
JacobHockey13

People who like this

1 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 JacobHockey13 · Jul 23, 2014 at 11:05 PM 0
Share

Thank you. That was quick!

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

2 People are following this question.

avatar image avatar image

Related Questions

A Button, A Boolean and 2 Textures. 2 Answers

GUI.Button not responding when drawn in a Rect. 2 Answers

Function with multiple if, else if statements. 1 Answer

If button highlighted 2 Answers

How to make a GUI appear and disappear with the same button? 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