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 .