Walljump if next to a wall - Returning NullReferenceException

Hey guys. I’m currently trying to add a walljump to my little test game. I simply want to check if a character jumps next to a wall and presses jump again in mid air, and have them boost away from the wall.

I’ve got the raycasting done, located in the Raycast script file, and it sends a variable specifying left or right boosting based on where the wall is to the jumpCheck script.

The problem I get is something I am not sure about, so I need some help. This is my current script for jumpCheck:

var sideHitting : int;

sideHitting = 0;

function Update () {
	if (gameObject.GetComponent(CharacterMotor).CharacterMotorJumping.holdingJumpButton == false  gameObject.GetComponent(CharacterMotor).CharacterMotorJumping.jumping == true  sideHitting == 0){
		if (Input.GetKeyDown(KeyCode.Space)){
			if(sideHitting == 1){
				Debug.Log("LEFT");
				//Boost Left
				sideHitting = 0;
			}
			if(sideHitting == 2){
				Debug.Log("RIGHT");
				//Boost Right
				sideHitting = 0;
			}
		}
	}
}

For this I am returned an error that says: “NullReferenceException: Object reference not set to an instance of an object” along with a lot of other jargon. It ends, “checkJump.Update () (at Assets/Script/checkJump.js:6)”.

I’m assuming I am not referencing the CharacterMotor properly or something like that. But I am still very new to this so I am not sure of the correct way.

If anyone can help me out I would be most grateful.

Thanks!

You should probably put all those gameObject.GetComponent(CharacterMotor) calls into a variable, say charMotor, then replace all the calls with that variable. And before you actually use charMotor check that it isn’t null.

Hey Wahooney thanks for the reply. I did like you said and set some vars up to constantly grab the values in the characterMotor script.

Unfortunately I’m still having some issues. I can trace the values and they show up. But they aren’t CHANGING for some reason. Do I have the wrong variables, or do they even change? I’m referencing “jumping” and “holdingJumpButton” vars from the characterMotor script. Do these hold the information that their names suggest?

var sideHitting : int;
var charMotorHoldJump : boolean;
var charMotorJumping : boolean;

sideHitting = 0;

function Update () {

charMotorHoldJump = gameObject.GetComponent(CharacterMotor).CharacterMotorJumping.holdingJumpButton;
charMotorJumping = gameObject.GetComponent(CharacterMotor).CharacterMotorJumping.jumping;

if (charMotorHoldJump== false  charMotorJumping == true  sideHitting == 0){
	if (Input.GetKeyDown(KeyCode.Space)){
		if(sideHitting == 1){
			Debug.Log("LEFT");
			//Boost Left
			sideHitting = 0;
		}
		if(sideHitting == 2){
			Debug.Log("RIGHT");
			//Boost Right
			sideHitting = 0;
		}
	}
}
}

As always, any help is very appreciated!

Thanks