i’m working on a new menu and i need the buttons to become uninteractable when the player hasn’t unlocked the levels yet. I’ve had a script for this for a while now and in the old scene/menu it works perfectly and gives no errors but in the new scene/menu it gives a NullReferenceException: Object reference not set to an instance of an object on line 27 and none of the buttons become uninteractable.
I’m using the new unity 4.6
Here is the script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LevelSelectScript : MonoBehaviour {
private int levelIndex;
GameObject Button;
void Start (){
CheckLockedLevels();
}
//Level to load on button click.
public void Selectlevel(string Level){
Application.LoadLevel("TransferData"+Level); //load the level
}
//Checks if the buttons are interactable or not
void CheckLockedLevels (){
for(int j = 1; j < LockLevel.levels; j++){
levelIndex = (j+1);
if((PlayerPrefs.GetInt("level"+levelIndex.ToString()))==1){
Button = GameObject.Find("Level"+(j+1));
Button.GetComponent<Button>().interactable = true;
}
if((PlayerPrefs.GetInt("level"+levelIndex.ToString()))==0){
Button = GameObject.Find("Level"+(j+1));
Button.GetComponent<Button>().interactable = false;
}
}
}
}
The new buttons have the same names as the old buttons in the editor.