• 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 LandonC · Mar 19, 2017 at 01:04 AM · optimizationswitchenumif-else

Optimising if else statements to switch and enums, or are there better methods?

Hello, I've hard coded 21 bools to check in my if else statements. Currently i want to optimize them with switch and enums.

To illustrate, here's an example:

 void Activate(Stat stat)
 {
         if(stat.box || stat.item)
         {
                   Debug.Log("take item");
                    return;
         }
         else
         {
                  if(stat.light)
                  {
                          stat.light.enabled;
                          return;
                   }
                  else if(stat.enemy)
                  {
                           stat.enemy.Appear();
                  }
         }
 }

It goes even lengthier complicated with the rest of the bools integrated. So here's what I plan to do:

 enum StatType {Box = 0, Item = 0, Enemy = 0, Light = 0};
 
 void Start()
 {
         ObjectStat();
 }
 
 StatType ObjectStat()
 {
          if(stat.box)
          {
                   ObjectStat.Box = 1;
          }
          if(stat.item)
          {
                   ObjectStat.Item = 1;
          }
          if(stat.light)
          {
                   ObjectStat.Light = 1;
          }
          if(stat.enemy)
          {
                   ObjectStat.Enemy = 1;
          }
 }
 
 void Activate()
 {
         switch(ObjectStat)
         {
                    case ObjectStat.Enemy:
                             if(ObjectStat.Light == 0)
                             { 
                                       stat.enemy.Appear();
                              }
                              break;
                    case ObjectStat.Box:
                            //Debug.Log("take the item");
                              break;
                    case ObjectStat.Item:
                            //Debug.Log("take the item");
                              break;
                    case ObjectStat.Light:
                              stat.light.enabled = true;
                              break;
         }
 }

Will this be more efficient? Or are there other methods that I'm not aware of? Any ideas or tips are much appreciated! :D

Comment

People who like this

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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Commoble · Mar 19, 2017 at 01:07 AM

If you're doing what I think you're doing -- you have a few different object types (Enemy, Box, Item, and Light) and you want them to behave differently, but you want the same thing to activate all of these behaviors, then I suggest making each of these a subclass of some base class. Then you can treat all of these objects the same way, but define the specifics of what they do in their own class.

Comment
Bunny83
LandonC

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 Bunny83 · Mar 19, 2017 at 01:54 AM 1
Share

Or maybe just use an interface which each different class implements. Base classes usually restrict you too much and couples the classes too much.

Though the bit we have seen of the actual setup is not really enough to pin point or recommend a concrete solution. It highly depends on the actual goal. That's the general problem with questions that only focus on a particular solution for a problem without mentioning the actual problem.

avatar image LandonC Bunny83 · Mar 23, 2017 at 02:38 AM 0
Share

I see, I'm only doing this to make my function less expensive. But I get the idea. I'm not quite sure how to use enums to be honest, at first I thought it is a set of variables that could be use individually but turns out it doesn't work that way. I think setting them as different classes would do a better job, thanks for all the advice!

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

error BCE0022: Cannot convert error, please help 3 Answers

How to set a value for each level without using a switch statement 2 Answers

How to check if an enum’s case has changed 4 Answers

What is a more efficient way to write this Switch Statement? 3 Answers

Enum Method vs Switch 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