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 /
  • Help Room /
avatar image
1
Question by C8H11N02 · Nov 07, 2019 at 01:04 PM · loopif-statementsdynamicvaluecollect

How to collect all the values of (if) in one ?

I have C# code where there are many conditional if statements. How can i collect all the values of in one ?

 void Update () {
             if(Score.score == 10)
             {
                 X.text = "X" + 1;
                 anim.Play("X");
             }
     
             if(Score.score == 30)
             {
                 X.text = "X" + 2;
                 anim.Play("X");
             }
     
             if(Score.score == 50)
             {
                 X.text = "X" + 3;
                 anim.Play("X");
             }
     
             if(Score.score == 70)
             {
                 X.text = "X" + 4;
                 anim.Play("X");
             }
     
             if(Score.score == 90)
             {
                 X.text = "X" + 5;
                 anim.Play("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

3 Replies

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

Answer by Zaeran · Nov 07, 2019 at 01:44 PM

For this particular case, you can use

 if(Score.score % 20 == 10)

That will let you know if a score is a 10 higher than a multiple of 20 (10, 30, 50, etc.)

You can then just use integer division to set the X.text value.

 X.text = "X" + ((Score.score / 20) + 1)

This will set the value to score / 20 with no remainder (10 / 20 = 0. 30 / 20 = 1, etc.), and adds one to get the value you're after.

In full, it should look like this:

 if(Score.score % 20 == 10){
     X.text = "X" + ((Score.score / 20) + 1)
     anim.Play("X");
 }
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 C8H11N02 · Nov 07, 2019 at 02:03 PM 2
Share

Thank you very much.

avatar image
0

Answer by Marioooo · Nov 07, 2019 at 01:43 PM

Hello!

if statements are used to do something if a condition is accomplished...

ifs and else is used for certain cases like yours this way:

 if (condition)
 {
     do something
 }
 else 
 {
     do something
 }

if you want to concat multiple if Statements then do this:

 if (condition)
 {
     do something
 }
 else if (condition)
 {
     do something
 }
 else
 {
     do something
 }

this way if some of the above ifs occurs, the code beneath the if statement that accomplished will be executed... if no other if statement is true, then the code on "else" will be executed....

i will do your code like this:

  void Update () 
 {
              if (Score.score == 10)
              {
                  X.text = "X" + 1;
                  anim.Play("X");
              } 
              else if (Score.score == 30)
              {
                  X.text = "X" + 2;
                  anim.Play("X");
              }
              else if (Score.score == 50)
              {
                  X.text = "X" + 3;
                  anim.Play("X");
              }
              else if (Score.score == 70)
              {
                  X.text = "X" + 4;
                  anim.Play("X");
              }
              else if (Score.score == 90)
              {
                  X.text = "X" + 5;
                  anim.Play("X");
              }
 }

you don't have an else statement because you don't have a default behaviour... so you don't need it...

NOW.... idk why you are doing this, you must play an animation when a player reaches a score of 10, 30, 50, 70 or 90? Idk where do you change the score, but is better practice to apply this changes there and play the animation there instead of using update

good luck!

Comment
Add comment · 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
0

Answer by petur · Nov 07, 2019 at 01:47 PM

I think you are looking for a "switch" statement: https://www.dotnetperls.com/switch

However, I would have use a more general case checking if score is between 1 and 90 and (score - 10) is a multiple of 20 (so 10, 30, 50, 70 and 90), and then using the score value to update the label.

It is not as legible as your series of ifs or a switch, but it is easier to add or change values.

 if ( Score.score > 0 && Score.score <= 90 && ( Score.score + 10) % 20 == 0 )       
 {
     x.text = "X" + ( (Score.score + 10) / 20 );
     anim.Play("X");
 }
Comment
Add comment · 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

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

117 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Break the loop when there are no more questions 0 Answers

Changing a value over time 1 Answer

Do Something If Temperature Reading Changes 0 Answers

Check for Shift in an If Statement, RTS giving several commands 0 Answers

Cursor Lock Code w/ if, else if, and else Statement Errors 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