Index was outside the boundary of array? Impossible! Help me please.

It said index was outside the range of boundary, when I call the function enterlvl_2, 5, and 6. The weird part is enterlvl 3 and 4 is able to call successfully. I dont know why, any idea?

public class GameManager : MonoBehaviour
{

    public GameObject[] level;
    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 == 0)
            {
                level.GetComponent<Button>().interactable = false;
            }
            else if (lvlunlock == 1)
            {
                level.GetComponent<Button>().interactable = true;
            }
        }

    }


    public void StartLvl()

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

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

    #region levelunlock
    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);
    }
    #endregion levelunlock


}

line 56,74 and 80 got the "index was outside the bound of array "error. I do know why there is such error for only 3 buttons .

Two things:

  1. please use code formatting. See first post in forum

  2. please post precise text of error, and indicate above which line it happens on

thanks for reminding, I dont know about that. I have formatted it.

When you check “lvlunlock” of a level, you have to include the index of the current iteration of the loop. Also i dont see why your index for the loop starts at 1 (Arrays start at 0, so an array with 6 elements (levels) has a length of 5 (including 0)
Your code:
[quote=“overpowerogue, post:1, topic: 765380, username:overpowerogue”]
```

  •    for (int i = 1; i < level.Length; i++)
      {
          Debug.Log(lvlunlock.Length);
    
          if (lvlunlock == 0)
          {
              level.GetComponent<Button>().interactable = false;
          }
          else if (lvlunlock == 1)
          {
              level.GetComponent<Button>().interactable = true;
          }
      }*
    

* *[/quote] * *Proposed code:* *

  •    for (int i = 0; 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;
          }
      }*
    

```
Without the specific index for the fields you want to check, the programm does not know which element of the array it has to check/modify.

The reason I put i=1 is because there is no need to unlock level 1 button since it is unlocked initially, thats why I start from 1.
I still get the error even added

Neither line 56 nor 74 in your code access an array, line 80 doesn’t exist. If you give us bad quality info, all you get are bad quality answers. I for sure can only guess, and my guess is that even through you declare ‘level’ as an array of GameObject, you never initialize it, so it’s empty. Accessing Level[3] will of course create an out of bounds error.