Why am I getting this error?

I have a script that’s supposed to change the XOffsset in my camera script. Here’s the code…

var aimX: float = 0.7;
var normalX: float = 0.8;
private var camera: Camera;
function Start(){
     chMotor = GetComponent(CharacterMotor);
     var ch:Camera = GetComponent(Camera);
}
function Update(){
     var cameraX = normalX;
     if (Input.GetButton("Aim")){
         cameraX = aimX;
      }
      camera.MouseOrbitOTS.XOffest = cameraX;
}

But when I go into play mode, I get this error…

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)

Why am I getting this error? Its supposed to change the XOffset in my custom mouse orbit script from 0.8 to 0.7. If you have any questions, just ask.

You didn’t define the camera variable anywhere, so it’s null. You also declared a “ch” variable in Start, but then do nothing with it.

–Eric

In line 3 you created a field called camera of type Camera. You never assigned it so it is null. Also in like 6 you created a local variable and assigned it the component of Camera, the issue is since you created the variable ch in a method it has local scope so it gets destroyed after Start() {} completes.

So what I think you need to do is fix line 6 to
camera = GetComponent(Camera);

  1. You never declare variable “chMotor”, you only assign it.
  2. You never assign variable “camera”, you only declare it.
  3. MouseOrbitOTS isn’t a member of Camera.

SOLUTIONS

  1. var chMotor = GetComponent(CharacterMotor)
  2. In your start method, replace “ch” with “camera”
  3. No idea.