Hi,
I keep getting the following error:
NullReferenceException: Object reference not set to an instance of an object
Turrent_AI.SearchTarget () (at Assets/Turrent_AI.cs:56)
Turrent_AI.FixedUpdate () (at Assets/Turrent_AI.cs:22)
But I can’t find the flaw in my code. Maybe someone knows?
Here’s my code:
void SearchTarget (){
Collider newTarget = null;
Collider[] collidersInRange= Physics.OverlapSphere(transform.position,range);
foreach(Collider currentCollider in collidersInRange)
{
Collider currentEnemy = currentCollider;
float healthCur = currentEnemy.gameObject.GetComponent<Stats>().health;
float healthNew = newTarget.gameObject.GetComponent<Stats>().health;
if (newTarget == null){
newTarget = currentEnemy;
}
else if (healthNew < healthCur){
newTarget = currentEnemy;
}
}
if (newTarget != null)
target = newTarget.transform;
}