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??