Error "NullReferenceException" - why?

Hey guys! I hope you can help me. I think everything should just work fine but I get the following 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)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
PlayerController.setAnimation () (at Assets/Scripts/PlayerController.js:64)
PlayerController.Update () (at Assets/Scripts/PlayerController.js:28)

My code:

@script RequireComponent(CharacterController)
@script RequireComponent(SpriteController)

public var gravity = 0.5;
public var speed = 5.0;
public var jumpPower = 10.0;

private var characterControl : CharacterController;
private var spriteControl: SpriteController;
private var moveDirection: Vector3;
private var velocity;
private var jump;
private var lookRight;

function Start () {
	characterControl = GetComponent(CharacterController);
	spriteControl = GetComponent(SpriteController);
	moveDirection = Vector3.zero;
	velocity = 0.0;
	jump = false;
	lookRight = true;
}

function Update () {
	inputCheck();
	move();
	setAnimation();
}

private function inputCheck() {
	velocity = Input.GetAxis("Horizontal") * speed;
	if (Input.GetKeyDown(KeyCode.Space)) {
		jump = true;
	} else {
		jump = false;
	}
}

private function move() {
	if (characterControl.isGrounded) {
		if (jump) {
			moveDirection.y = jumpPower;
		}
	}

	moveDirection.x = velocity;
	moveDirection.y -= gravity;
	characterControl.Move(moveDirection * Time.deltaTime);
}

private function setAnimation() {
	if (velocity > 0) {
		lookRight = true;
		//Walk right
		spriteControl.setAnimation(SpriteController.Animations.WALK);
	} else if (velocity < 0) {
		lookRight = false;
		//Walk left
		spriteControl.setAnimation(SpriteController.Animations.WALK);
	} else {
		if (lookRight) {
			//Stand right
			spriteControl.setAnimation(SpriteController.Animations.FRONT);
		} else {
			//Stand left
			spriteControl.setAnimation(SpriteController.Animations.FRONT);
		}
	}
	
	if (!characterControl.isGrounded) {
		if (lookRight) {
			//Jump right (Overwrite)
			spriteControl.setAnimation(SpriteController.Animations.JUMP);
		} else {
			//Jump left (Overwrite)
			spriteControl.setAnimation(SpriteController.Animations.JUMP);
		}
	}
}

The error is in line 64 of your original script which is line 49 in your post. The only reasons are:

  • your GameObject doesn’t have a SpriteController component.
  • You might have destroyed the SpriteController component.