Instance of Object error

Hello All,

I have a peculiar error. I hope I can’t just chalk it up to the “I’m doing it wrong” category, but you never know.

I have imported a scene that a co-worker and I have been working on. On his computer, everything runs perfectly. I have imported his scene with all of his scripts dependencies and prefabs. The scene is extremely simple with just a single first person controller. I am trying to lock the mouse in a first person controller. We are handling this by disabling the MousLook script. In the project that the controller was built in, the code works perfectly. However, In my project, I receive an error message when calling the following:

GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;
GameObject.Find("Main Camera").GetComponent("Raycasting").enabled = false;

this generates the following error. Note there is only a single Main Camera on the screen and the same GameObject.Find call below works perfectly to disable the Raycasting script.

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.SetProperty (System.Object target, System.String name, System.Object value)
TrashShooterController.Update () (at Assets/Scripts/ControlScripts/TrashShooterController.js:43)

However, when I use the ml2, which i linked by dragging and dropping the script from the inspector, I get no error. The example of the code is:

var ml2: MouseLook; //a mouselook script linked to the main camera
ml2.enabled = false;
//GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;
GameObject.Find("Main Camera").GetComponent("Raycasting").enabled = false;

Am I missing some kind of dependency that was not exported? Or is this a type of bug? I have got the controller to work just the way I want with the ml2 link, but I just wanted to know if I was missing something for future reference should this error occur again.

A NullReference means that the script went to find a reference variable (If you don’t know what that means, don’t worry right now. You’ll just get more confused) and there was not a reference to any thing. Basically, your script looked for Main Camera and couldn’t find it. You could either use Camera.main to find it, or I don’t think there is a space between main and camera.

The problem is, if it couldn’t find “Main Camera” I would be receiving two errors. At the moment it’s only giving me the error for the first call to the .GetComponent(“MouseLook”); I have a hunch that it might be a conflict of the MouseLook script. Maybe it lost it’s reference to it in the export or I have a duplicate MouseLook that is somehow messing with it.

The damnedest thing is I created a new project to begin combining our assets and scenes with our artists. And the import worked just fine. So I think the problem would have to do with my own project scripts.

I would suggest breaking your problem line of code up, and adding some debug logging to figure out what’s going wrong. When you say

GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;

What happens is:

  • GameObject.Find() is called, and returns either a game object or null
  • GetComponent() is called on the returned GO, and returns either a component or null
  • The enabled property on the returned component gets set

The NullReferenceException indicates that either (a) Find() returned null, resulting in an attempt to call GetComponent() on a null reference; or (b) GetComponent() returned null, resulting in an attempt to set the ‘enabled’ property on a null reference. The problem is, you can’t tell which of those failures has occurred. So, step one, break the operations out so you can tell which fails:

var go = GameObject.Find("Main Camera");
var c = go.GetComponent("MouseLook")
c.enabled = true;

The line number of the NullReferenceException() will then tell you where the problem is. Then you can investigate why you’re getting a null reference. Some possible reasons:

  • There isn’t an object named “Main Camera”; the Find() returns null
  • There is an object named “Main Camera”, but it doesn’t have a MouseLook component; the GetComponent() returns null
  • There is an object named “Main Camera” which does have a MouseLook component, but there’s another object with the same name that doesn’t have a MouseLook component, and Find() is returning the wrong one

The key here is to break your code out, so exceptions uniquely identify the source of the error and you have the intermediate results up to that point; and then to us Debug.Log() to investigate the intermediate results and find out what value is different than you were expecting.

Thanks laurie, that’s probably what I should do. I’ll get to work on that ASAP.