Not all paths return a value
I am making a function that detects if a collision has occurred, found if somewhere and don't understand it much, it appears to return a value, but for some reason the unity editor provides me with a "Not all code paths return a value" error
my code is included below, if anyone can aid me in fixing it or even cleaning it up, i would be most grateful
using UnityEngine; using System.Collections; using UnityEngine.UI; using Random = UnityEngine.Random;
public class Mspawn : MonoBehaviour { public Button button; public BoardManager boardManger; public LayerMask BlockingLayer;
private float x;
private float y;
void OnEnable(){
if (Input.GetMouseButtonDown(0)) {
button.onClick.AddListener (SpawnUnit);
}
}
void Awake(){
boardManger = GetComponent<BoardManager> ();
}
void SpawnUnit()
{
x = randomSpawnPosition ().x;
y = randomSpawnPosition ().y;
//checkposition ();
for (int i = 0; i < boardManger.PlayerUnits.Length; i++)
{
if (boardManger.PlayerUnits[i].CompareTag("PlayerSoldierMarksman"))
{
if (checkposition(randomSpawnPosition()))
{
}
Instantiate (boardManger.PlayerUnits[i], new Vector3 (x,y,0f), Quaternion.identity);
}
}
}
//This is the problem function, this is where the error occurs
public bool checkposition(Vector3 SpawnedPosition)
{
GameObject[] AllActive = GameObject.FindGameObjectsWithTag ("PlayerHQ");
foreach (GameObject active in AllActive)
{
if (active.transform.position == SpawnedPosition)
{
return false;
}
else {return true;}
}
}
Vector3 randomSpawnPosition()
{
float x = Random.Range(0,2);
float y = Random.Range(0,2);
Vector3 Position = new Vector3 (x, y,0f);
return Position;
}
}
This is a standard C# error -- nothing to do with Unity. Do a Search on that phrase and look in (non-Unity) places for all the explanation you'd ever want.
Answer by meat5000 · May 17, 2016 at 03:41 PM
Didnt read sorry. Based on the title you must make sure that in a function which expects a return value, if you have if else statements or any other kind of branching you must make sure to include a return statement at the end of each branch.
OK I had a little look.
What about the case in which there are 0 GameObjects in AllActive? Both return statements will be skipped.
public bool checkposition(Vector3 SpawnedPosition)
{
GameObject[] AllActive = GameObject.FindGameObjectsWithTag ("PlayerHQ");
foreach (GameObject active in AllActive)
{
if (active.transform.position == SpawnedPosition)
{
return false;
}
else {return true;}
}
// In the event your iterable is empty
return true;
}
You've fixed it syntactically, but the error in the OP is that the return true
is in the wrong place, logically, and it's been left there. This fix, like the OP, only checks the first object in AllActive (it returns from the function regardless of what it finds there). So while it compiles, the logic's still wrong.
Looks to me like the else { return false; }
line should be removed. Then it'll check all of them and only return true if none of them is at SpawnPosition.