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
0
Question by Bandicoot3D2Y · Sep 30, 2016 at 10:28 AM · if statementelse

How would you shorten that piece of code?

Just a quick question. I have a piece of Code and i dont know how i can shorten that better. How would you compress that?

 IEnumerator ActivatePickUp()
 {
     if (Random.Range(0,2) == 0)
     {
         if (shotAmount < 5)
         {
             shotAmount ++;
             yield return new WaitForSeconds(PickUpTime);
             shotAmount --;
         }
         else
         {
             if (fireRate > 0.075)
             {
                 fireRate /= 2;
                 yield return new WaitForSeconds(PickUpTime);
                 fireRate *= 2;
             }
         }
     }
     else
     {
         if (fireRate > 0.075)
         {
             fireRate /= 2;
             yield return new WaitForSeconds(PickUpTime);
             fireRate *= 2;
         }
         else
         {
             if (shotAmount < 5)
             {
                 shotAmount ++;
                 yield return new WaitForSeconds(PickUpTime);
                 shotAmount --;
             }
         }
     }
 }
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

Answer by troien · Sep 30, 2016 at 11:17 AM

Well, as in literal lines of code, you could use "else if" at a few places..

Like so:

 IEnumerator ActivatePickUp()
 {
     if (Random.Range(0,2) == 0)
     {
         if (shotAmount < 5)
         {
             shotAmount ++;
             yield return new WaitForSeconds(PickUpTime);
             shotAmount --;
         }
         else if (fireRate > 0.075)
         {
             fireRate /= 2;
             yield return new WaitForSeconds(PickUpTime);
             fireRate *= 2;
         }
     }
     else if (fireRate > 0.075)
     {
         fireRate /= 2;
         yield return new WaitForSeconds(PickUpTime);
         fireRate *= 2;
     }
     else if (shotAmount < 5)
     {
         shotAmount ++;
         yield return new WaitForSeconds(PickUpTime);
         shotAmount --;
     }
 }

Normally you could then combine duplicate code into seperate methods and call these methods instead of continuously using the duplicate code. But considering you have just a limited piece of duplicate code and because yield return is a part of your duplicate code (which is annoying as it would mean that any seperate method would require to be a ienumerator aswell). I don't really see any benefits of doing that as in this use case it would really just make the code less readable...

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
2

Answer by Bunny83 · Sep 30, 2016 at 11:29 AM

You mean something like this?

 IEnumerator ActivatePickUp()
 {
     bool c1 = (fireRate > 0.075);
     if (((Random.Range(0,2) == 0) || !c1)&& (shotAmount < 5))
     {
         shotAmount ++;
         yield return new WaitForSeconds(PickUpTime);
         shotAmount --;
     }
     else if (c1)
     {
         fireRate /= 2;
         yield return new WaitForSeconds(PickUpTime);
         fireRate *= 2;
     }
 }
Comment
Add comment · Show 3 · 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 tanoshimi · Sep 30, 2016 at 11:42 AM 1
Share

This doesn't handle the case when Random.Range(0,2) == 1 && fireRate <= 0.075 && shotAmount < 5 (the very bottom case in the OP's code)

avatar image Bunny83 tanoshimi · Sep 30, 2016 at 11:44 AM 0
Share

I've realised this the moment i posted my answer. I've already changed my answer about 3 $$anonymous$$utes before your comment ^^.

avatar image Bunny83 · Sep 30, 2016 at 11:51 AM 2
Share

This is pretty much the "shortest" way. However i'm not sure if that has any advantages. It makes it harder to read / understand and it doesn't improve anything neither performance nor code complexity.

avatar image
0

Answer by MrEastwood · Sep 30, 2016 at 03:12 PM

This ended up actually being longer, but the following piece of code has no duplication of logic, that is you have one place where you can change the rules for each condition.

 void ActivatePickUp()
 {
     if (Random.Range(0,2) == 0)
     {
         if (!tryShotAmount()) tryFireRate();
     }
     else
     {
         if (!tryFireRate()) tryShotAmount();
     }
 }
 
 bool tryShotAmount()
 {
     if (shotAmount < 5)
     {
         StartCoroutine(ActivateShotAmount());
         return true;
     }
     return false;
 }
 
 bool tryFireRate()
 {
     if (fireRate > 0.075)
     {
         StartCoroutine(ActivateFireRate());
         return true;
     }
     return false;
 }
 
 IEnumerator ActivateShotAmount()
 {
     shotAmount ++;
     yield return new WaitForSeconds(PickUpTime);
     shotAmount --;
 }
 
 IEnumerator ActivateFireRate()
 {
     fireRate /= 2;
     yield return new WaitForSeconds(PickUpTime);
     fireRate *= 2;
 }
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

6 People are following this question.

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

Related Questions

I have a specific problem with my trigger 0 Answers

Why doesn't my "else" statement fire? 1 Answer

EASY FIX! How to make the screen flash for only a few seconds? 3 Answers

if statement wont execute on health < 0; 0 Answers

if statement not working correctly with keyinput 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