Bool's value won't return to false, why?
I know this sounds like a simple problem, but I can't find the solution no matter what. I just simply wanna do something like this:
public class Player: Monobehaviour
{
public GameObject targetEnemy;
private bool inCombat;
void Update ()
{
if (targetEnemy != null)
{
inCombat = true;
} else
{
inCombat = false;
}
}
}
And here is the method that will return the targetEnemy:
public class EnemySensor: Monobehaviour
{
private List<GameObject> interactors = new List<GameObject>();
private Player player;
void Start()
{
player = transform.parent.gameObject.GetComponent<Player>();
}
void Update()
{
player.targetEnemy = GetClosestEnemy ();
}
public GameObject GetClosestEnemy()
{
if (interactors.Count != 0)
{
GameObject closest = null;
float minDistance = Mathf.Infinity;
foreach (GameObject enemy in interactors)
{
float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
if (distance < minDistance)
{
closest = enemy;
minDistance = distance;
}
}
return closest;
} else
{
return null;
}
}
}
Everything work just fine, when some NavMesh enemies entering my EnemySensor trigger at once, the script adds them to interactors List, and removes them on trigger exit. After that the GetClosestEnemy() method returns the closest enemy to Player's targetEnemy. The targetEnemy field which is visible in inspector showing an Enemy GameObject when closest enemy got returned, and None (GameObject) when no enemies are currently being detected by EnemySensor. But my inCombat bool stays true all the time even when targetEnemy field showing "None (GameObject)." Help? Many thanks in advance!
Answer by I_Am_Err00r · Aug 07, 2019 at 02:16 PM
Just a suggestion, but try moving this:
foreach (GameObject enemy in interactors)
{
float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
if (distance < minDistance)
{
closest = enemy;
minDistance = distance;
}
}
return closest;
to something like this:
foreach (GameObject enemy in interactors)
{
float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
if (distance < minDistance)
{
closest = enemy;
minDistance = distance;
return closest;
}
}
else
{
return null;
}
I don't have a compiler to confirm if that would work, but I see you returning closest outside of your distance check in the foreach loop which I think is causing the issue; so the way I'm thinking is if you run that distance check, and an enemy is less than the max distance, you return that enemy there, or if there is nothing within that distance, you return null after that distance check.
Again, don't have Unity or a compiler, so please check for syntax errors and you might need to move the 'return null;' around to get it just right (maybe add another, I'm not a whiz at coding and can't say definitively without testing if my solution returns all paths). Let me know if I'm in the ballpark to helping you solve this!
Unfortunately, I got a compiler error "not all code paths return a value" by doing this
This line:
I'm not a whiz at coding and can't say definitively without testing if my solution returns all paths
I'll help you, but learn how to look stuff up on your own so that you can fix a simple error like this yourself:
public GameObject GetClosestEnemy()
{
if (interactors.Count != 0)
{
GameObject closest = null;
float $$anonymous$$Distance = $$anonymous$$athf.Infinity;
foreach (GameObject enemy in interactors)
{
float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
if (distance < $$anonymous$$Distance)
{
closest = enemy;
$$anonymous$$Distance = distance;
return closest;
}
}
else
{
return null;
}
}
}
else
{
return null;
}
}
I might have the placement wrong (can't confirm without a compiler) and I'm literally writing this on the Unity forum, so please move that last else {return null;} around to find out what path doesn't return a value.
Your answer
![](https://koobas.hobune.stream/wayback/20220612210212im_/https://answers.unity.com/themes/thub/images/avi.jpg)