- Home /
Tagged objects not returning value
Hi all!
I got some code from the documentation and I'm evidently using it wrong as I get the error 'not all code paths return a value'. I'm trying to get the furthest player object (as there are multiple) from my game object to open a door.
I'm using the .FindObjectsWithTag documentation which is second down here: https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
The 'Bee' tagged objects can all be destroyed however I attempted to mitigate this with the 'null' sections.
public GameObject FurthestBee()
{
GameObject[] bees = GameObject.FindGameObjectsWithTag ("Bee");
if (bees != null) {
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag ("Bee");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
Vector3 diff = go.transform.position + position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
return closest;
// Debug.Log (closest);
}
}
if (bees == null)
{
return null;
}
}
I'm pretty new to all this, any help would be really appreciated
Cheers ya beautiful people!
Your answer
Follow this Question
Related Questions
IndexOutOfRangeException help on debug 3 Answers
how can i check if index exists? 2 Answers
Object reference not set to an instance of an object error C# 1 Answer
How can I make this GameObjects array remain permanent when i activate? 1 Answer
error CS0236: A field initializer cannot reference the nonstatic field, method, or property 1 Answer