- Home /
"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++;
}
}
I didn't think it was possible to accept two answers any more.
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.
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
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
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
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.
@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
![](https://koobas.hobune.stream/wayback/20220613185636im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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