- Home /
NullReferenceException
I keep getting a NullReferenceException whenever I am trying to disable a script from another script. The exact error is NullReferenceException: Object Reference not set to an instance of an object. I really don't understand what is going on and it's really getting on my nerves, any help would be appreciated. Code is below.
var information: String;
var script: MouseLookPlus;
var script2: MouseLookPlus;
private var guiOn = false;
private var rect: Rect;
function MouseToggle () {
script = GetComponent(MouseLookPlus);
script.Enabled = false;
script2 = GetComponent(MouseLookPlus);
script2.Enabled = false;
}
function OnMouseDown () {
guiOn = true;
rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
MouseToggle();
}
function OnGUI () {
if (guiOn){
GUI.Label(rect, information);
if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
guiOn = false;
}
if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
animation.Play ("dooropen1");
guiOn = false;
}
}
}
I have two Script variables because there are two scripts I'm attempting to disable when the GUI comes on.
It would be helpful if edited your quetion and comment the line that is causing the error. Without you showing us, we have to guess. GetComponent() as you've used it here gets a component on the current game object. Are you sure the $$anonymous$$ouseLookPlus script is attached to the same game object as this script?
Agreed, please cut and paste the actual error - it should contain the line number which will make it much easier for us to help you
This is the actual error I get in my Debug Log, and no the mouselook scripts are tied to the main camera, and first person controller, while the script that is executing the command is tied to the object which produces the GUI
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
openscript1.$$anonymous$$ouseToggle () (at Assets/$$anonymous$$isc Scripts/OpeningScripts/openscript1.js:14)
openscript1.On$$anonymous$$ouseDown () (at Assets/$$anonymous$$isc Scripts/OpeningScripts/openscript1.js:23)
UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
Looks like
script = GetComponent($$anonymous$$ouseLookPlus);
...is failing. I would do:
if(script == null) Debug.Log("SCRIPT IS NULL!");
...right after line 14 to confirm.
Answer by gribbly · Mar 24, 2013 at 09:45 PM
Oh yeah, robertbu got it right. GetComponent will only work like that if MouseLookPlus is attached to the same object as this script.
You need to do something like:
script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
...basically get hold of your camera object and call GetComponent on that.
Thank you so much, after days of searching it finally worked! Should make this so much easier now. Thanks so much!!!
Unfortunately, I have now reached another problem. See I have multiple objects that create a GUI. So once I try to add the coding to stop the movement when the GUI comes on, none of them except one of the objects work. Basically the way it works is that I have several doors that open pending the answer of their respective questions. But when I apply the camera disabling script to the second door, the first door GUI stops disabling the camera script. Any ideas?
GUI 1: var information: String;
var script: $$anonymous$$ouseLookPlus;
var script2: $$anonymous$$ouseLookPlus;
private var guiOn = false;
private var rect: Rect;
function $$anonymous$$ouse1 () {
script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
script.enabled = false;
script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
script2.enabled = false;
}
function $$anonymous$$ouse11 () {
script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
script.enabled = true;
script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
script2.enabled = true;
}
function On$$anonymous$$ouseDown () {
guiOn = true;
rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
}
function OnGUI () {
if (guiOn){
$$anonymous$$ouse1();
GUI.Label(rect, information);
if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
guiOn = false;
}
if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
animation.Play ("dooropen1");
guiOn = false;
}
}
else {
$$anonymous$$ouse11 ();
}
}
GUI 2: var information: String;
var script: $$anonymous$$ouseLookPlus;
var script2: $$anonymous$$ouseLookPlus;
private var guiOn = false;
private var rect: Rect;
function $$anonymous$$ouse2 () {
script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
script.enabled = false;
script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
script2.enabled = false;
}
function $$anonymous$$ouse22 () {
script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
script.enabled = true;
script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
script2.enabled = true;
}
function On$$anonymous$$ouseDown () {
guiOn = true;
rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
}
function OnGUI () {
if (guiOn){
$$anonymous$$ouse2();
GUI.Label(rect, information);
if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
guiOn = false;
}
if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
animation.Play ("dooropen2");
guiOn = false;
}
}
else {
$$anonymous$$ouse22 ();
}
}
Please accept the answer that worked for you, and post a new question for this entirely separate issue!