I have been finding the errors for day, but still no clue!!

I appreciate very much if somebody could help me fix this, I am exhausted… The error occurred when I want to call each of the function enter_lvl the most down part of script. You guys can reference from that, would be much easier to understand the script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{

    public GameObject[] level;   // 6 gameobjects referenced
    float[] lvlunlock = new float[6];



    void Awake()
    {
        lvlunlock[1] = PlayerPrefs.GetFloat("lvl2_unlock", 0);
        lvlunlock[2] = PlayerPrefs.GetFloat("lvl3_unlock", 0);
        lvlunlock[3] = PlayerPrefs.GetFloat("lvl4_unlock", 0);
        lvlunlock[4] = PlayerPrefs.GetFloat("lvl5_unlock", 0);
        lvlunlock[5] = PlayerPrefs.GetFloat("lvl6_unlock", 0);
  


      
      
            for (int i = 1; i < level.Length; i++)
            {
                Debug.Log(lvlunlock.Length);

                if (lvlunlock[i] == 0)
                {
                    level[i].GetComponent<Button>().interactable = false;
                }
                else if (lvlunlock[i] == 1)
                {
                    level[i].GetComponent<Button>().interactable = true;
                }
            }
        }

  


    public void StartLvl()

    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

  public void quit()
    {
        Application.Quit();
    }

 
    public void enterlvl_2()
    {
        level[1].GetComponent<Button>().interactable = true;
        PlayerPrefs.SetFloat("lvl2_unlock", 1);
    }
    public void enterlvl_3()
    {

        level[2].GetComponent<Button>().interactable = true;
        PlayerPrefs.SetFloat("lvl3_unlock", 1);
    }
    public void enterlvl_4()
    {

        level[3].GetComponent<Button>().interactable = true;
        PlayerPrefs.SetFloat("lvl4_unlock", 1);
    }
    public void enterlvl_5()
    {

        level[4].GetComponent<Button>().interactable = true;
        PlayerPrefs.SetFloat("lvl5_unlock", 1);
    }
    public void enterlvl_6()
    {

        level[5].GetComponent<Button>().interactable = true;
        PlayerPrefs.SetFloat("lvl6_unlock", 1);
    }
 


}

error at line59,77 and 83. Index was outside the bounds of the array.
The index are all within the range though. Why this error occurred??

Probably because the index is outside the bounds of the array.

The level array is probably empty. It’s probably empty because this is happening in a different GameManager instance than the one you think it’s happening in. Try to log the array size before you get the exceptions.

When you get an index out of bounds, you haven’t found a bug in the C# runtime. You’ve got a bug in your code.

i have debug log the level array, and stated that 6 objects inside. And if it is empty, the other function would not be called successfully too.

How should I log the size inside the code?

    public void enterlvl_6()
    {
        Debug.Log($"{gameObject.name}: level size={level?.Length}");
        level[5].GetComponent<Button>().interactable = true;
        PlayerPrefs.SetFloat("lvl6_unlock", 1);
    }

Note what if level is list, not array, you must use .Count instead of .Length