Array index is out of range?

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.

Ok…So I Debug.Log the health variable and it is 3 which is what it should be at the start of the game. But right after this is called:

health = health - 1;

the debug reads -15… Why is this happening?

I take it you aren’t logging directly before and after that line. Something is happening in between your logs.

I just tried logging before the health = health - 1 and it is already a negative number before I even subtract from it. I don’t understand how. Everything to do with the it I’ve posted in this post.

Here is the really screwy thing… If I add Debug.Log(health) in the GameManager’s Update function it constantly stays at 3 even after getting hit by an asteroid…it’s like it ignores:

health = health - 1;
livesArray[health].SetActive (false);

and just instantiates the player without error.

I don’t know what is going on…