- Home /
Weird NullReferenceException
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.
the only advice I'd give (and probably you'd done it 1k times already) is to check the names of the button gameObjects starting from the Level2 to Level[whatever], that you don't misspell the names, case-sensitivity is considered... :/
Answer by DubStepMaster · Dec 03, 2014 at 11:46 AM
try to add an if statement to ask, if the button != null
if(Button != null){
Button.GetComponent<Button>().interactable = false;
}
hope it works
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Trigger player movement on touch 1 Answer