Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 CB-TV · Sep 06, 2013 at 04:28 PM · variablefunctionif-statements

++ Changes variable to 121?

I know exactly why this is happening. My function is OnGUI. I have my simple script saying that when a button is pressed it adds a variable +1. It adds 121 or some times 50 - it's random. I know why it happens - because it measures how long the button is pressed for. I'm guessing that the "if (GUI.Button..." statement is set for when the mouse it pressed, not let go. Does anyone know how to make it so when you click the button, it adds 1 only???? Thanks!!!

Comment
Add comment · Show 2
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 perchik · Sep 06, 2013 at 04:30 PM 0
Share

Post your code and we'll help

avatar image CB-TV · Sep 06, 2013 at 04:30 PM 0
Share

This works:

  if (GUI.Button (Rect(10,10,50,50), "Click?"))
    Respect +=1;
    print(Respect);

This doesn't:

  if (GUI.Button (Rect(10,10,50,50), "Click?"))
    print(Respect);
    Respect +=1;

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by perchik · Sep 06, 2013 at 04:32 PM

You need curly braces around an if statement that is more than one line.

 if (GUI.Button (Rect(10,10,50,50), "Click?"))
 {
    print(Respect);
    Respect +=1;
 }


The first example works because without curly braces, the first line after the if statement is the body of the statement

That is

 if (GUI.Button (Rect(10,10,50,50), "Click?"))
    Respect +=1; //the if statement applies to this line
    print(Respect); //but not this line

but here

 if (GUI.Button (Rect(10,10,50,50), "Click?"))
 {  //with curly braces
    Respect +=1; //the if statement applies to this line
    print(Respect); //the if statement applies to this line too
 }


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 CB-TV · Sep 06, 2013 at 05:14 PM 0
Share

Sorry, I actually did have curly braces in it but didn't paste them onto the comment!

avatar image perchik · Sep 06, 2013 at 05:15 PM 0
Share

So you're saying if you print the variable first, it doesn't get changed?

avatar image gardian06 · Sep 06, 2013 at 06:24 PM 1
Share

the variable gets changed (you told it to, by putting Respect +=1), but if the print happens before the addition then the print will grab the value at that specific point in time, and then the modification will happen. take for example the following examples

 if (GUI.Button (Rect(10,10,50,50), "Click?")){
     respect = 10
     respect += 1;
     // respect = 11
     print(restect); // console shows 11
 }
 
 
 if (GUI.Button (Rect(10,60,50,50), "Click?")){
     respect = 10
     print(respect); // console shows 10
     respect += 1;
     // respect = 11
 }
 
 if (GUI.Button (Rect(10,110,50,50), "Click?")){
     respect = 10
     print(respect); // console shows 10
     restpect += 1;
     // respect = 11
     print(respect); // console shows 11

sometimes it is important to know the value of a variable both before, and after a change (usually for more complex changes, but that is beside the point)

avatar image perchik · Sep 06, 2013 at 06:25 PM 0
Share

D'oh. I shoulda caught that too.

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

17 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

Related Questions

Problem with increasing a variable 1 Answer

Writing PropertyDrawers for functions. 1 Answer

Lower player's health from separate script. 2 Answers

Accessing a variable within a function.... GetComponent() 2 Answers

change variable name inside IF statement, or add text to it 3 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