repost due to the mess of code uploaded here just now.
I had checked for several time, no any of my index is out of bounds, I really lost here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject[] level; //6 referenced gameobjects
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();
}
#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
}
error at line 56,74 and 80 . I can call the function of enterlvl 3 and 4 without problem, only those, thats really weird. Help me out,Thanks in advanced!