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 /
avatar image
0
Question by shamblinwithshotguns · Jan 01, 2018 at 06:24 PM · arrayloopbooleanbool

Not all code paths return a value in boolean loop

This is the loop that is giving me a problem. I am trying to make it so that if all requirements in an array of booleans are true, or if there are no requirement booleans in the array, the master boolean returns true. Otherwise, if not all requirement booleans are true the master boolean returns false. I got the code to work somewhat as a void, but if the last boolean in the array returned true it caused the master boolean to return true as well.

     private bool AllRequirements {
         get {
             for (int i = 0; i < Requirement.Length; i++)
             {
                 if (Requirement[i] == true)
                 {
                     return true;
                 }
                 else if (Requirement[i] == false)
                 {
                     return false;
                 }
             } 
         }
     }
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

2 Replies

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

Answer by Dragate · Jan 01, 2018 at 07:52 PM

What it's telling you is that you need to ENSURE the function will return something. The compiler sees that you only return a value when certain conditions are met. What happens if neither of those 2 if-statements are true?! What does the function return?!

Aside from that, you got a logical error. You return a value from the very 1st array element (which is ok IF you found a req not met). You're not checking the rest of the elements.

I would resolve this with the below code. Basically, code will return early if it finds a req not met. Otherwise (all reqs met), it will reach the end of the loop and return true.

 private bool AllRequirements {
    get {
       for (int i = 0; i < Requirement.Length; i++){
           if (Requirement[i] == false){
              return false;
           }
       } 
       return true;
    }
 }
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 Bunny83 · Jan 01, 2018 at 08:05 PM

You have several problems here. Some logic and some semantic problems. Even if Requirements is a bool array, in which case your two conditions are mutually exclusive, it's still possible that your array is empty. So the for loop wouldn't run at all. In this case you don't return anything. This is impossible for a method with a return type \ property getter. You always must return a value.


Next thing is you do not check if all Requirements are true. Your logic only checks the first one. Just imagine there are 3 requirements. At index 0 you first check if that requirement is true and if so you immediately return true without checking the others. However if the first is false your second if would become true and you immediately return false.


What you probably want is something like this:

 private bool AllRequirements {
      get {
          for (int i = 0; i < Requirement.Length; i++)
          {
              if (Requirement[i] == false)
              {
                  return false;
              }
          }
          return true;
      }
  }

As soon as you hit one requirement that is false we terminate the loop and return false. However if the loop manages to get through all Requirements we know all have to be true. It will also return true when there is no requirement at all.

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

83 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

Related Questions

How can I do a foreach loop for an array of booleans? 1 Answer

How can I move back to Element 0 in an array to reset a loop? 1 Answer

question about array or list of boolean 1 Answer

Check if boolean is true on my gameObjects from my array 1 Answer

How to add a Sprite Rollover Image using a Button Array 0 Answers


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