(UnityEngine.Transform)': not all code paths return a value

Line(19) (UnityEngine.Transform)': not all code paths return a value
Any help is always greatly appreciated.

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);
		while(closeWaypoints.Length < 3  breakWhile <10)
		{
			closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
				
			if(closeWaypoints.Length >0)
			{
				return closeWaypoints[0].gameObject;	
			}

			
			Dis += 10;
			breakWhile++;
		}	
	}

What the function is supposed to return if closeWaypoints.Length equals to 3?

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’;

getting WaypointOne’ is assigned but its value is never used
(19)UnityEngine.Transform)': not all code paths return a value

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++;
		}	
	}

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

    }

That’s so much simpler than adding ‘return null’ to the end of the function…

Or throw an Exception :stuck_out_tongue:

Thanks for the help