- Home /
Help with my code?(NullReferenceException)[CLOSED]
I keep getting this error with my code.
NullReferenceException: Object reference not set to an instance of an object Gui.OnGUI () (at Assets/Essential Scripts/Gui.js:15)
It is on this line:
if(script.selected1 == true)
Here is my code:
var rType = 0;
var script : Selecting_System;
function OnGUI() {
if (GUI.Button(Rect(10,70,50,30),"Click")){
rType = 1;
Debug.Log("Clicked the button with text");
}
if(rType == 1)
{
if(script.selected1 == true)
{
Debug.Log("Success");
}
}else{
Debug.Log("Else");
}
}
I'm trying to get it so that when I click the gui button the variable "rType" is equal to 1. Then I want it so when the variable from another script(selected1) is true and the variable "rType" is equal to 1 it will debug "success" It keeps giving me the error i typed above. If you could critique my script and/or explain why I am getting this error that would be great. Thanks in advanced.
Is the variable "selected1" set to public in your Selecting_System script? Can we see how your Selecting_System script looks?
Also, your if(rType == 1) statement should just be bundled with it's child if statement so you only have one "if", to simplify the code. Since the first statement isn't executing anything that the second statement would need, you're just checking if both are true before saying "Success".
if((rType == 1) && (script.selected1))
{
Debug.Log("Success");
}
Answer by ArkaneX · Jan 06, 2014 at 11:06 AM
In the line you posted, NullReferenceException can be caused only by null script
variable. So most probably you didn't set this variable in the inspector. Or, less likely, you set it to null somewhere else in your code.
Answer by United_Unity · Jan 06, 2014 at 03:39 AM
First Check your spelling sounds stupid i know but that is my worst problem
second what are you doing with var script : selecting_system shouldn't that be
var script = selecting_system
and i believe you are missing a closing Bracket }
but i may very well be wrong with every thing is just said new to unity but know javascript