create a GameObject before the ‘while’ execution and initialize it as null; then at the end of the code return the variable you have set.
At the if(closeWaypoints.Lenght > 0 ) chage the return to variable_you_created = closeWaypoints[0].gameObject; and add a break; to stop the ‘while’;
If you create a function that returns something, it MUST return something always, look that the ONLY return in your function is inside of a if-clause, so if it is false, it will never return. You must guarantee that there is always one execution-path that goes until a return line, look this code at it’s very end I put a return line, in any case you’ll reach one of the following return lines.
public LayerMask mask = 1 << 8;
public int Dis = 10;
private int breakWhile = 1;
public Collider[] closeWaypoints;
public GameObject FindClosestWaypoint(Transform inTransform)
{
var inPosition = inTransform.position;
closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
GameObject WaypointOne = null;
while(closeWaypoints.Length < 3 breakWhile <10)
{
closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
if(closeWaypoints.Length >0)
{
return WaypointOne = closeWaypoints[0].gameObject;
}
Dis += 10;
breakWhile++;
}
return null; // null = empty, nothing to return
}