• 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 Jan_Julius · Aug 27, 2014 at 10:52 AM · switchcase

checking if 2 out of 4 is true no matter which one

Let's say that I have a switch statement like so

     switch(id){
     case 1:
         break;
     case 2:
         break;
     case 3:
         break;
     case 4:
         break;
     }

How can I detect in an easy manner without overloading my script with tons of code wether 2 out of these 4 are true, may it be 1 and 2 or 2 and 3 or 4 and 1, whenever 2 are true I need something to happen how can I achieve very easily and cleanly? (I know how to do it but I want a nice and clean way if possible)

Thanks!

Comment

People who like this

0 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 Kiwasi · Aug 27, 2014 at 11:01 AM 0
Share

Pleas note overloading is a specific technical term in computer programming. What you are doing has no relevance to overloading.

avatar image jokim · Aug 27, 2014 at 03:39 PM 1
Share

how can your "id" be equal to 2 different value at the same time ?

I think you need to be a little bit more precise about your question... How are you using it ?

avatar image robertbu · Aug 27, 2014 at 05:38 PM 0
Share

Clean code is sometimes more a matter of formatting than approach. With four bool, there are only six states:

 if (   ( a &&  b  && !c  && !d)
     || ( a && !b  &&  c  && !d)
     || ( a && !b  && !c  &&  d)
     || (!a &&  b  &&  c  && !d)
     || (!a &&  b  && !c  &&  d)
     || (!a && !b  &&  c  &&  d)
    

) {

And if the conditions was 'at least 2' rather than 'exactly 2', the condition would be substantially simpler.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Kiwasi · Aug 27, 2014 at 10:59 AM

Pseudo code

 private bool[] bools = new bool[4];
 private int count = 0;
 
 foreach(bool myBool in bools){
     if(myBool){
         count++;
     }
 }
 if (count>=2){
     return true;
 } else {
     return false;
 }


Comment
AngryBurritoCoder
Paulo-Henrique025

People who like this

2 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 Paulo-Henrique025 · Aug 27, 2014 at 05:48 PM 1
Share

You can also add them to a list if you want to use named vars instead of an array :)

avatar image Kiwasi · Aug 27, 2014 at 07:55 PM 0
Share

That's true. Foreach will run on any collection type.

avatar image

Answer by Ellandar · Aug 27, 2014 at 11:09 AM

Hi Jan,

I think the problem here is you want something that switch cannot provide. Basically switch is shorthand for an if-elseif block of code, thus your id variable can only be equal to a single case at any run through.

Without much more info, I can only give you a possible solution that may work.

 int trueCounter;
 
 if(id == case1)  trueCounter++;
 if(id == case2)  trueCounter++;
 if(id == case3)  trueCounter++;
 if(id == case4)  trueCounter++;
 
 if(trueCounter == 2)
 {
    // your stuff happens here
 }




Comment

People who like this

0 Show 0 · 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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Switch case using enum javascript 2 Answers

add case in swicth, from editor unity 0 Answers

Why does only one object respond, while more objects have exact same script? 0 Answers

Is it bad coding to have a switch statement with 300 cases in update()? 1 Answer

Updated Question if statement not executing case 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