- Home /
NullReferenceException!! Please Give a Hand
Hi, all Unity Hero, I have a question to execute the following script which is the unlock system by PlayerPrefs, NGUI is used as plugins, The Unity shows me the following
,"NullReferenceException unlock system1.Update () (at Assets/Scripts/unlock system1.js:19)"
and I have no any clue to solve it, please kindly give me a hand.
#pragma strict
var levelReached1 : int =0;
var levelReached2 : int =0;
var levelReached3 : int =0;
levelReached1 = PlayerPrefs.GetInt("1");
levelReached2 = PlayerPrefs.GetInt("2");
levelReached3 = PlayerPrefs.GetInt("3");
function Update(){
var S1B : UIButton = GameObject.Find("S1 Button").GetComponent(UIButton);
var S2B : UIButton = GameObject.Find("S2 Button").GetComponent(UIButton);
var S3B : UIButton = GameObject.Find("S3 Button").GetComponent(UIButton); <-- Here
if(levelReached1 == 1)
{S1B.active = true;
S2B.active = true;}
else if (levelReached1 ==0)
{S1B.active = true;
S2B.active = false;}
if(levelReached2 == 2)
{S2B.active = true;
S3B.active = true;}
else if (levelReached2 ==0)
{S2B.active = true;
S3B.active = false;}
if(levelReached3 == 3)
{}
else if (levelReached3 ==0)
{}
}
Insert around line 20:
Debug.Log(S1B + " - " + S2B + " - " + S3B);
I'm guessing that one or more of these is not being found. Carefully check to make sure the name in the Find() and the game object name are an exact match.
Re: Robertbu
Thx for your kindly help, I have added the Debug.Log on line20, However, the Null Reference Exception is still appear from Unity. I have checked all of the name are excatly the same with the Game Object, any hints for tackling this problem?
@yanebra - I've converted your answer to a comment. The answer field is for actual answers. By using it for comments you indicate that your question has been answered and fewer people will view it.
So did the Debug() statement indicate that any of the three was null? The only way I can see that this code will produce the error you describe is if the Find() fails or if the game object does not contain a UIButton component. Note that even an extra space in the name or a different letter case will cause the Find() to fail.
@robertbu $$anonymous$$any thx to you, I have fixed it, thx to guide me in a right way. Happy develope to you!
Your answer
![](https://koobas.hobune.stream/wayback/20220613101030im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
NullReferenceException: Object reference not set to an instance of an object. 2 Answers
NGUI Action Bar Questions 0 Answers
PlayerPrefs HighScore problems, it doesn't work. 4 Answers
Help with NullReferenceException 1 Answer