• 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 /
This question was closed Jan 08, 2015 at 06:47 PM by UnityKen for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by UnityKen · Jan 08, 2015 at 04:28 PM ·

Multiple If Statements

Hi,

I'm not really sure how to explain what I want so I hope this makes sense. If I had some code similar to this and need to create this multiple times (50+), how would I do it without writing it out every time. It would be multiple if statements with just two numbers different each time. The first if statement would have two values of 1, the second would have two values of 2, the third would have two values of 3 and so. I could write out all 50+ if statements but thought there could be an easier way other than that.

 {
     if Health == (PlayerHealth - 1)
     {
         Something = 1;
     }
     if Health == (PlayerHealth - 2)
     {
         Something = 2;
     }
     if Health == (PlayerHealth - 3)
     {
         Something = 3;
     }
     if Health == (PlayerHealth - 4)
     {
         Something = 4;
     }
     if Health == (PlayerHealth - 5)
     {
         Something = 5;
     }
 }

Thanks

Comment
Add comment · Show 3
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 tanoshimi · Jan 08, 2015 at 04:34 PM 0
Share

None of your statements will ever be true. What possible value of "Health" could there be such that Health == Health - 1?

avatar image UnityKen · Jan 08, 2015 at 04:38 PM 0
Share

I'm sorry for not explaining things better but correcting the code isn't my problem, what I'm trying to do is create multiple if statements with just a slight change in each statement but without wring it out every time.

avatar image Scribe · Jan 08, 2015 at 05:04 PM 0
Share

just as a point, if there is an obvious linear connection between what you are checking and what you are setting, finsing the equation is normally much easier and quicker. e.g. in the example above you could do:

 Something = PlayerHealth - Health;

possibly clamping the answer to something sensible, that would do the same thing as your example but for all values -1, -2, -3...

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by DaDonik · Jan 08, 2015 at 04:55 PM

This should work and is by far less stuff to write:

 int iNumberOfIfStatements = 5;
 
 for (int i = 0; i < iNumberOfIfStatements; ++i)
 {
     if (Health == (PlayerHealth - i))
     {
         Something = i;
     }
 }
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 Anxo · Jan 08, 2015 at 05:04 PM 0
Share

Thumb up for your answer, only works if the action is always the same but if so, its the better approach.

avatar image UnityKen · Jan 08, 2015 at 05:49 PM 1
Share

It works perfectly, thanks for the help.

avatar image
1

Answer by Anxo · Jan 08, 2015 at 04:35 PM

First, how could Health == Health -5? that would never be true.

I think what you are looking for is a switch statment.

 switch(Health){
     case(1):
     Something = 1;
    break;
 
   case(2):
   Something = 2;
   break;
 }

Its like saying "check out Health... in case its 1 do this and break out of the switch statement, if not move on, in case its 2 do this and break out of the switch statement.

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 UnityKen · Jan 08, 2015 at 04:51 PM 0
Share

I have just changed the code so hopefully it will make more sense, but correcting the code isn't what I'm trying to do. Using case's might be what I'm looking for so i'll do some research on that, Thanks for the help.

avatar image Anxo · Jan 08, 2015 at 05:10 PM 0
Share

The ForLoop that DaDonik posted is the best answer for your code, the benefit of a switch statement is that you can change what happens if the condition is different like so.

 switch (girlsName){
    case("Amanda"):
    $$anonymous$$iss();
    break;
 
    case("Suzi"):
    AskForNumber();
    Debug.Log("Suzi is hot");
    break;
 
    case("Linda"):
    Debug.Log("Linda's Boyfriend is crazy");
    bool boyfriendIsHere = CheckForBoyfriend;
    if(boyfriendIsHere){
      WalkAway();
    }
    else {
     AskForNumber();
     }
     break;

     default:
     AskForName();
    break;
 }
avatar image Scribe · Jan 08, 2015 at 05:15 PM 0
Share

if the example given in the question is accurate to the actual problem then DaDonik's answer is definitely not the best, why check iNumberOfIfStatements if statements every frame when you can calculate it with one line Something = PlayerHealth - Health;

avatar image DaDonik · Jan 08, 2015 at 05:15 PM 0
Share

Also a thumbs up for you, because you invested the time of pointing out a more general solution that was not requested by the OP.

avatar image
1

Answer by chillersanim · Jan 08, 2015 at 05:04 PM

If the relation between the numbers is linear or maps an arithmetic function, you could do something like this:

 something = health - playerhealth;

Else you can use a dictionary to map certain numbers to other numbers:

 // Populate the dictionary with all relations you need
 private Dictionary<int, int> somethingDictionary = new Dictionary<int, int>
 {
     {0,0}, 
     {1,1}, 
     {2,4} 
 };
 
 // Returns the something that belongs to the given health and playerhealth
 public int GetSomething(int health, int playerhealth)
 {
     var index = health - playerhealth;
     return somethingDictionary[index];
 }

For more informations about Dictionaries: See here

Hope one of this solutions can help you.

Greetings
Chillersanim

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 Scribe · Jan 08, 2015 at 05:09 PM 0
Share

that first line was exactly my thought, why is everyone making their own job harder! Lazy coders who find the easy way are the best :D

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

29 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

Related Questions

I Need to maintain a regular speed w/h sprint script 0 Answers

Health C# script problem. 3 Answers

Changing variables in another script 3 Answers

I need to find a Script and Disable/enable it.. 1 Answer

Script Default References vs. Object Variable References? 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