Singleton and array problems

I’m trying to print the number in an array by modifying an int using a singleton.
not sure if this is the best method but i’m just experimenting.
i’m getting this error:
IndexOutOfRangeException: Index was outside the bounds of the array.
Health.Update () (at Assets/Health.cs:23)

public class GameManager : MonoBehaviour
{
    public int Health = 0;
    public static GameManager instace;
    //int a = 5;
   
    // Start is called before the first frame update
    private void Awake()
    {
        instace = this;
      
    }
    // Update is called once per frame
    void Update()
    {
      
    }

    public void AddHealth(int health)
    {
        Health = Health + health;
       
    }
}

public class Health : MonoBehaviour
{
    public int[] arr = { 0, 2, 4, 7, 9 };
    public int array;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameManager.instace.AddHealth(1);
            //Debug.Log(GameManager.instace.Health);
            int array = GameManager.instace.Health;
            Debug.Log(arr[array]);
        }
    }


   
}

This thread belongs in the Scripting forum, this isn’t the place.

Have you tried first printing the int itself? Or better yet, connecting VS to Unity and stepping through the method to see what happens at each step? If you stop it right before your debug call you can see the int value, as well as the array’s value.

(btw naming your int “array” is a terrible idea)

It is highly likely that you first had an empty array on the component, then added it to the object, then edited the code to add {…} elements, and as a result right now when you run this code, your array is empty .

Why? Because the old value serialized and now overwrites the default you provided.

Try to reset the component, re-aad it, or use “static readonly” array if the numbers never change.