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 decerto · Aug 12, 2012 at 11:24 PM · ifif statementnumbersstatement

if statement help!

Hi there!

Please excuse the title of this thread, I really didn't know what to call it!

Anyway:

I have a timer ticking in seconds that decrease a variable called "thirst". When "thirst" reaches a certain number, it will display on the GUI however this only works for the numbers it's given.

 if(thirst == 100){
 GUI.Label (Rect(40, Screen.height - 70,60,60)," Hydrated", style2);
 }
 
 if(thirst == 80){
 GUI.Label (Rect(40, Screen.height - 70,60,60)," Dry", style3);
 } 
 
 if(thirst == 60){
 GUI.Label (Rect(40, Screen.height - 70,60,60)," Thristy", style4);
 } 
 
 if(thirst == 40){
 GUI.Label (Rect(40, Screen.height - 70,60,60)," Dehydrated", style5);
 }
 
 if(thirst == 20){
 GUI.Label (Rect(40, Screen.height - 70,60,60)," Parched", style6);
 }
 
 if(thirst == 10){
 GUI.Label (Rect(40, Screen.height - 70,60,60)," Dying", style7);
 }

It's only showing for when the variable hits 100,80,60,40,20,10 - how can i make it show for 99,98,97 all the way down to 0 without having 100 if statements?

I hope this is clear, Thanks in advance!

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

2 Replies

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

Answer by hathol · Aug 12, 2012 at 11:46 PM

Ok, first thing: there is a thing called "else" or "else if" respectively. Syntax is

 if(A)
 {
     // do something if A is true
 }
 else if(B)
 {
     // do something if A is not true and B is true
 }
 else
 {
    // do something if neither A or B are true
 }

you also have the <= (less than or equal) and >= (greater or equal) operators (checks for exactly that) Now with that in mind, you could write

 if(thirst <= 10)
 {
     //for 0 - 10
 }
 else if(thirst <= 20)
 {
     // for 11 to 20 since 0-10 has already been excluded
 }
 else if(thirst <= 30)
 {
    // 21 to 30
 }
 . // you get the point
 . // I skiped 31 to 80 cause I'm lazy ;)
 .
 else if(thirst <= 90)
 {
     // 81 to 90
 }
 else
 {
     // 91 and above
 }
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 decerto · Aug 12, 2012 at 11:53 PM 0
Share

Thank you! You have been a massive help!

avatar image
3

Answer by Loius · Aug 12, 2012 at 11:39 PM

<=

or

 var states : String[] = ["Dying", "Parched", "Parched", "Dehydrated", "Dehydrated", "Thristy", "Thristy", "Dry", "Dry", "Hydrated", "Hydrated"];
 var styles : {type of styles}[]; // fill in in Inspector, could do this for states too
 
 ...
 
 var thirstIndex : int = thirst * 0.1; // will convert thirst from 0-100 to 0-10
 GUI.Label( {xxxx}, states[ thirstIndex ], styles[ thirstIndex ] );

Gotta love arrays.

Comment
Add comment · Show 2 · 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 OperationDogBird · Aug 12, 2012 at 11:59 PM 0
Share

Awesome approach, very methodical

avatar image Loius · Aug 13, 2012 at 05:37 AM 0
Share

Sometimes I think my brain's been replaced by a robot, but then I forget how to type and I'm reasonably sure it's still in its place.

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

10 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

Related Questions

How to make several conditions for an if statement? 5 Answers

if else statement doesnt work 1 Answer

Need help with multiple if statements. 3 Answers

if moving statement 1 Answer

Can I Put a IF In a IF?!? 2 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