Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 borkov · Mar 09, 2015 at 11:03 PM · loopforeach

"not all code paths return a value" i really try it and i really don't have idea

"not all code paths return a value" why ?

 Transform nextWayPoint(Transform currentWaypoint)
 {
     int ik = 0;
     foreach(Transform waypoint in trackWayPoints)
     {
         if(waypoint == currentWaypoint)
         {
             return nextWayPointinArray(ik);
         }
         ik++;
     }
 }
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 meat5000 ♦ · Mar 10, 2015 at 12:40 AM 0
Share

I didn't think it was possible to accept two answers any more.

avatar image aditya · Apr 19, 2016 at 12:00 PM 0
Share

how did this guy accepted two answers at a time ....

3 Replies

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

Answer by Cherno · Mar 09, 2015 at 11:11 PM

What happens if trackWayPoints is empty? Then the iteration wouldn't even reach the first if(waypoint ==... statement. So, you need to add a "return null" line at the very end of the function, or return fallback transform instead of null.

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
Best Answer

Answer by CaKeMeaT · Mar 09, 2015 at 11:14 PM

if either your foreach fails, or if() returns false, both don't reach the return

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 aga · Mar 09, 2015 at 11:36 PM

You're getting the error because your method will not return any value if the "if(waypoint == currentWaypoint)" does not trigger at all.

If you're sure that the "if" statement will trigger, although I wouldn't recommend it, you can do the following workaround: Add this after your foreach loop:

 return transform;


Also since you're using the counter "ik" anyway I'd suggest using a for loop instead, like so:

 Transform nextWayPoint(Transform currentWaypoint)
     {
         for (int i = 0; i < trackWayPoints.Length; i++)
         {
             if (trackWayPoints[i] == currentWaypoint)
                 return nextWayPointinArray(i);
         }
         return transform;
     }

Note that if "trackWayPoints" is a List< Transform > you have to replace .Length with .Count

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 borkov · Mar 10, 2015 at 12:07 AM 0
Share

trackWayPoints is array and thx for answer i already figured this out but last return never been reaching really and i don't know why it's recuired because fallback and "index is out of range" error i have in nextWayPointinArray method so 2nd return in nextWayPoint method never can't be reaching because before getting parameter with over limit index code stops at nextWayPointinArray method when trying sending back trackWayPoints[5] when trackWayPoints[] have 4 index's

avatar image aga · Mar 10, 2015 at 12:28 PM 0
Share

If I understand your question correctly, then my answer is this: If your method has a return type, within that method you have to guarantee that it has something to return in every possible scenario.

To give you an example of when your method is invalid: Your method will not have anything to return if "currentWaypoint" is not in your array. It doesn't matter if it's impossible - your method doesn't know that. It only cares that a value is returned. This is where the second return comes in. It will ensure that even if your foreach loop does not return anything, the method still has a "plan B" to fall back on.

Hope this helps.

avatar image borkov · Mar 10, 2015 at 08:02 PM 0
Share

@aga So every non void method need to have return value out of any if's and loops just for put them through debugger ?

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

25 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

Related Questions

foreach Gameobject in array problems 1 Answer

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

Guarantee loop order of child objects. 1 Answer

Foreach with GameObject.Find() 1 Answer

Making an AI Flee 2 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