I have an asteroid with that has a script attached to it that controls it and it has this segment of code attached to it:
if (other.tag == "Player")
{
Destroy (other.gameObject);
Destroy (gameObject);
GameManager.lives--;
if (GameManager.lives > 0)
{
gameManager.respawnPlayer ();
}
}
So it calls the respawnPlayer function in my GameManager class that has this:
public void respawnPlayer ()
{
if (!gameOver)
{
health = health - 1;
livesArray[health].SetActive (false);
Instantiate (player, playerSpawn, Quaternion.identity);
}
}
This all worked fine until I added the health an array. What I’m trying do is show lives via UI which I have done using a UI panel that has 3 images on it (life icons). I made a public array (livesArray) and filled it with these icons in the inspector. This works when I debug it to make sure the array is filled cuz in my GameManager start function I added this line:
health = livesArray.Length;
But I am getting this error: Array index is out of range. The line with SetActive is causing this and I am not sure why.
Let me know if you need me to explain any of this better to you guys.