NullReferenceException: Object reference not set to an instance of an object

Hello guys,
I got that message, to me very weird, in correspondence of these lines of code. The object “i_SwitchCameras” exists in the scene and in the hierarchy, so why when I try to use it, I get this NULL Reference execption?

Thanks.
:frowning:

var gg = GameObject.Find("i_SwitchCameras");
	
	if(i_MainGUIcamera) {
		if(gg)
			gg.GetComponent("i_SwitchCameras_Scripts").switchIso2Perspective();
	}

	return;
}

Does your i_SwitchCameras GameObject contain a component called “i_SwitchCameras_Scripts”?

yes it does :frowning:

That’s an interesting using of the if statement. GameObject.Find returns the name of the game object, yet you are testing if(true). Are you sure that will work?
I would normally assign the the GameObject.Find to an object and often the script it contains to another object and then use that script object directly.

eg (for a script attached to my current object)

@script RequireComponent(EncodeDecode)
private var myEncodeDecode : EncodeDecode;
myEncodeDecode = GetComponent(EncodeDecode);

–PW

Could you show me a screenshot of the objectinspector with the selected i_SwitchCameras GameObject?
Maybe including the tree of scripts you use.

@Launchpad: GameObjects have a bool operator, his is basically testing for != null: http://unity3d.com/support/documentation/ScriptReference/Object-operator_bool.html?from=GameObject

Here it is.

I’m testing if(gg) only to check if the gg is different from Null. I thought that the GameObject.Find(“…”) would return a GameObject with named that way.

Thanks,
GC>

569512--20266--$GUI.JPG

Couldn´t you post the whole script? And the Error Message including the StackTrace?

Ah yeah sorry: the stack says:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, System.Object[ ] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[ ] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args, System.Type scriptBaseType)
i_MainGUI.onGUI_SetIsoViewCamera () (at Assets/Standard Assets/iScripts/i_MainGUI.js:49)
i_MainGUI.buildGUI () (at Assets/Standard Assets/iScripts/i_MainGUI.js:27)
i_MainGUI.OnGUI () (at Assets/Standard Assets/iScripts/i_MainGUI.js:40)

function buildGUI(){
	GUI.skin = i_skin;
	var sx: int = 10;
	var sy: int = 25;
	
 
    GUI.tooltip = "";
	if (GUI.Button(Rect(sx,sy,32, 32),GUIContent("","Clean Scene"), "New")) {
	}
	GUI.Label(Rect(sx,sy+30,50,50), GUI.tooltip,"tooltip");
	
	GUI.tooltip = "";
	if (GUI.Button( Rect(sx + 40, sy, 32, 32), GUIContent("","Set iso-view"), "iso")){
			onGUI_SetIsoViewCamera();
	}
	
	GUI.Label(Rect(sx + 40, sy+30, 32, 32),GUI.tooltip,"tooltip");
	
};


//Function which design the GUI interface
function OnGUI() {
 GUI.skin = i_skin;
 
 if(b_showGUI)
	buildGUI();
}


//Sets the camera in Isometric View.
function onGUI_SetIsoViewCamera() {
var gg = GameObject.Find("i_SwitchCameras");

if(gg)
	gg.GetComponent("i_SwitchCameras_Scripts").switchIso2Perspective();

	return;
}

The line Numbers make no sense at all, is this the full script? And why does it call something with Boo?

I would for testing purposes write something like this:

function onGUI_SetIsoViewCamera() 
{
    var gg = GameObject.Find("i_SwitchCameras");
    print(gg ? "Found i_SwitchCameras" : "i_SwitchCameras not found");
    if(gg)
    {
        var component = gg.GetComponent("i_SwitchCameras_Scripts");
        print(component ? "Found i_SwitchCameras_Scripts" : "i_SwitchCameras_Scripts not found");
        if (component)
        {
            component.switchIso2Perspective();
        }
    }
}
var i_MainGUIcamera: Transform;
var i_skin : GUISkin;
private var b_showGUI; //Boolean for Enabling/Disabling the GUI

function Start() {

	b_showGUI = true;
}

function Update () {
}


function buildGUI(){
	GUI.skin = i_skin;
	var sx: int = 10;
	var sy: int = 25;
	
 
    GUI.tooltip = "";
	if (GUI.Button(Rect(sx,sy,32, 32),GUIContent("","Clean Scene"), "New")) {
	}
	GUI.Label(Rect(sx,sy+30,50,50), GUI.tooltip,"tooltip");
	
	GUI.tooltip = "";
	if (GUI.Button( Rect(sx + 40, sy, 32, 32), GUIContent("","Set iso-view"), "iso")){
			onGUI_SetIsoViewCamera();
	}
	
	GUI.Label(Rect(sx + 40, sy+30, 32, 32),GUI.tooltip,"tooltip");
	
};


//Function which design the GUI interface
function OnGUI() {
 GUI.skin = i_skin;
 
 if(b_showGUI)
	buildGUI();
}


//Sets the camera in Isometric View.
function onGUI_SetIsoViewCamera() {
var gg = GameObject.Find("i_SwitchCameras");

if(gg)
	gg.GetComponent("i_SwitchCameras_Scripts").switchIso2Perspective();

	return;
}

I have the same problem in this script today :frowning: where I press F1 and I want to switch from one camera to another changing the value of .enabled.
In this cases gg is a First Person Player… and isoCamera is the orthographic isometric camera. Again, I get the same message for the following statement:shock:

isoCamera.GetComponent("i_CameraControls").enabled = !isoCamera.GetComponent("i_CameraControls").enabled;
var isoCamera : Camera;

function Awake() {
	isoCamera.enabled = true;
	var gg = GameObject.Find("Player");
	
	if (gg){
		gg.GetComponent("FPSInputController").enabled = false;
		gg.GetComponent("CharacterMotor").enabled=false;
	}
}


function Update () {
}


function switchIso2Perspective() {
var gg = GameObject.Find("Player");

		isoCamera.enabled = !isoCamera.enabled;
		isoCamera.GetComponent("i_CameraControls").enabled = !isoCamera.GetComponent("i_CameraControls").enabled;
		isoCamera.GetComponent("DragRigidBodyShadow").enabled = !isoCamera.GetComponent("DragRigidBodyShadow").enabled;
	
		
		if (isoCamera.enabled == false) {
			isoCamera.GetComponent("i_CameraControls").SetStatus("perspective");
		} else 
			isoCamera.GetComponent("i_CameraControls").SetStatus("isometric");
		
		gg.GetComponent("FPSInputController").enabled = !gg.GetComponent("FPSInputController").enabled;
		gg.GetComponent("CharacterMotor").enabled = !gg.GetComponent("CharacterMotor").enabled;

}


function OnGUI(){

var e : Event = Event.current;

 
if(e.isKey) {

	if( (e.keyCode == KeyCode.F1)   (e.type == EventType.KeyUp)) {
		
			switchIso2Perspective();
		
		}
	
}

Could you also try what I wrote and show me the i_SwitchCameras_Scripts.js?

yes, it was successful! it could find the object named that way. :frowning: can’t understand…

for the second case it’s weird because I haven’t changed the code at all, and yesterday I wasn’t getting that error. :frowning:

Did you add “enabled” as a variable into your i_CameraControls script?

nope ther is no variable called like that. It’s part of the “Component” stuff… that what I knew.

For the second case I’ve found the mistake and I misspelled a key word. but still working on the first case :frowning:

Oh its part of MonoBehaviour, ok then.

Add more print(…)´s to locate the problem.

this is weird for the first case the

      var component = gg.GetComponent("i_SwitchCameras_Scripts");
        print(component ? "Found i_SwitchCameras_Scripts" : "i_SwitchCameras_Scripts not found");

print “NOT FOUND” :(( but the script is attached to the object!:frowning: