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 /
This question was closed Apr 06, 2016 at 10:35 PM by joshua-lyness for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by joshua-lyness · Apr 04, 2016 at 11:06 PM · errorerror messagefunctionreturnpaths

error "not all code paths return a value" in a function

Sorry, because I know this has been asked a hundred times before. But I have no idea why this code is giving me this error, because I cant see any path it could take for it to not return a value...

 Vector3 roadSnapping (Vector3 point){
     //goes through every road in the array, checks if snapping to end point
     for (int i = 0; i < controlPoints.GetLength (0); i++) {
         if (Vector3.Distance (controlPoints [i, 0], point) < 10) {
             return controlPoints [i, 0];
         } else {
             return point;
         }
     }
 }

The "for" loop is unavoidable, it will always go through this, unless the length is less than 1, however if I put a "return point;" after the loop, I get an error message calling it unreachable code. thanks for any help u can give :)

Comment
Add comment · Show 2
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 joshua-lyness · Apr 04, 2016 at 11:27 PM 0
Share
 ok so I even tried this...


 Vector3 roadSnapping (Vector3 point){
     //goes through every road in the array, checks if snapping to end point
     for (int i = 0; i < controlPoints.GetLength (0); i++) {
         if (Vector3.Distance (controlPoints [i, 0], point) < 10) {
             return controlPoints [i, 0];
         } else
             return point;
     } 
     if (controlPoints.GetLength (0) == 0) {
         return point;
     }
 }

now even the for loop not getting iterated is accounted for.... I'm lost...

avatar image Bunny83 joshua-lyness · Apr 04, 2016 at 11:38 PM 1
Share

You have to understand how a compiler actually deter$$anonymous$$es a condition. "GetLength" is a method which returns a dynamical value. The compile can't tell whats the valid range that method might return at runtime. Even such a case would produce the error:

 // this will produce an "not all code paths return a value" error
 public int Test()
 {
     bool b = true;
     if (b)
         return 3;
 }

"b" is a variable and it can be either true or false. "We" know that b is always true, but the compiler can't "think" and doesn't assume anything. The only thing that would work is a constant value:

 // this is valid as "true" will be always true
 public int Test()
 {
     if (true)
         return 3;
 }

 // this works as well as b is a constant here and not a variable.
 const bool b = true;
 public int Test()
 {
     if (b)
         return 3;
 }

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Apr 04, 2016 at 11:29 PM

As you said yourself the for loop only enters if the length of your array is greater than 0. Actually it doesn't matter what condition you use in the for loop unless it's a constant expression which can be determined at compile time which isn't the case here. A method that has a return value must always return a value. So you can't just leave the method without a "return" statement.

So you have to have a return after the for loop. I doubt that you get an "unreachable code" error unless you do something else wrong. This should work:

 Vector3 roadSnapping (Vector3 point)
 {
     for (int i = 0; i < controlPoints.GetLength (0); i++) {
         if (Vector3.Distance (controlPoints [i, 0], point) < 10)
         {
             return controlPoints [i, 0];
         }
     }
     return point;
 }

The else in your code doesn't make much sense as you would always exit the for loop in the first iteration so the for loop would be pointless.

Note: My example will return the first point which is closer than 10 units to the test point. Keep in mind that doesn't have to be the closest point. We don't know what you actually want to do so we can't suggest a specific solution.

Comment
Add comment · Show 7 · 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 joshua-lyness · Apr 04, 2016 at 11:31 PM 0
Share

ok, ill go try it again... functions give me a headache haha

avatar image joshua-lyness · Apr 04, 2016 at 11:33 PM 0
Share

right so I tried it again, and I don't know why it gives the unreachable code error (its a yellow one, not really an error.) well it points to the for loop, not even the return, so ill just ignore it. thanks, works I guess ;)

avatar image Bunny83 joshua-lyness · Apr 04, 2016 at 11:46 PM 0
Share

Well, if the compiler shows an "unreachable code" warning that that code is indeed unreachable. $$anonymous$$y example code doesn't produce that error, so you might have something else in your code that you didn't include in your question. For example an unconditional return before the loop or an additional for loop condition which always evaluates to false (at compile time).

unreachable code is pointless code. That means the compile can savely remove it as it doesn't make a difference if it's there or not.

avatar image joshua-lyness Bunny83 · Apr 04, 2016 at 11:48 PM 0
Share

its pointing right at the for loop. theres no other code that would conflict with that for loop in any way so I have no idea why its doing that... ill have another look at get back to you

Show more comments
Show more comments

Follow this Question

Answers Answers and Comments

48 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

Related Questions

Cannot import videos 0 Answers

Similar scripts but error on one and not the other 1 Answer

Visual Studio Code doesn't work for with unity 2 Answers

What is happening and what does this MEAN!?!?!?!?!?!?!?!? AHHHHH!!!!,WHAT IS HAPPENING?!?! 1 Answer

Change Automatic to Semi-Automatic 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