I don’t quite understand why I’m getting an “array index out of range” error.
I’ve a fairly simple code using a dynamic to activate enemies in certain zones:
public Transform thePlayer;
public GameObject[] enemy; // enemies to be activated using a dynamic array
public float distanceToZone;
void Update()
{
ActivateNme();
}
void ActivateNme()
{
//compares Distance(Vector3A, Vector3B);
float playerDistance = Vector3.Distance(thePlayer.position, transform.position);
if (playerDistance <= distanceToZone)
{
int enemyCount = (enemy.Length);
print(enemyCount);
enemy[enemyCount].SetActive(true);
}
}
I’m setting the size of the array in the inspector
The array index is there, yet I’m still getting this error.
Is there anything wrong with the syntax that’s causing this problem?
Thanks in advance