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

**URL:** https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562
**Category:** Physics
**Tags:** Scripting
**Created:** [February 2, 2013, 11:54pm UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562 "2013-02-02T23:54:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![1010](https://avatars.discourse-cdn.com/v4/letter/1/47e85d/32.png) [@1010](https://discussions.unity.com/u/1010)
#### Post date: [February 2, 2013, 11:54pm UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562/1 "2013-02-02T23:54:22Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![alexzzzz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexzzzz/32/333491_2.png) [@alexzzzz](https://discussions.unity.com/u/alexzzzz)
#### Post date: [February 3, 2013, 12:09am UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562/2 "2013-02-03T00:09:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![fano\_linux](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fano_linux/32/235288_2.png) [@fano\_linux](https://discussions.unity.com/u/fano_linux)
#### Post date: [February 3, 2013, 1:11am UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562/3 "2013-02-03T01:11:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![1010](https://avatars.discourse-cdn.com/v4/letter/1/47e85d/32.png) [@1010](https://discussions.unity.com/u/1010)
#### Post date: [February 3, 2013, 1:36am UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562/4 "2013-02-03T01:36:48Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![Kaze\_Senshi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kaze_senshi/32/235117_2.png) [@Kaze\_Senshi](https://discussions.unity.com/u/Kaze_Senshi)
#### Post date: [February 3, 2013, 1:41am UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562/5 "2013-02-03T01:41:52Z")

</div>

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.

```auto
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

    }

```

---

<div class="post-metadata">

### Author: ![npsf3000\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/npsf3000_1/32/466497_2.png) [@npsf3000\_1](https://discussions.unity.com/u/npsf3000_1)
#### Post date: [February 3, 2013, 1:42am UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562/6 "2013-02-03T01:42:41Z")

</div>

> [@fano\_linux](#):
>
> 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’;

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

> [@Kaze\_Senshi](#):
>
> If you create a function that returns something, it MUST return something always…

Or throw an Exception 😛

---

<div class="post-metadata">

### Author: ![1010](https://avatars.discourse-cdn.com/v4/letter/1/47e85d/32.png) [@1010](https://discussions.unity.com/u/1010)
#### Post date: [February 3, 2013, 2:15am UTC](https://discussions.unity.com/t/unityengine-transform-not-all-code-paths-return-a-value/496562/7 "2013-02-03T02:15:59Z")

</div>

Thanks for the help
