Hello, I’m very new to scripting and I’m trying to modify the bootcamp SoldierController script to work with my own soldier. I’ve aproached this by opening a new controller script and copying accross individual lines of code and trying to understand what they are doing as I go.
Currently I’m getting an error “NullReferenceException: Object reference not set to an instance of an object” from the Update function [from the line “if(!motor.canControl)”].
I understand that this error generally means that the script references something that doesnt exist, but:
- I’ve included the following line in the code:
private var motor : CharacterMotor; - I’ve included the following line in the “Start” function (which I don’t fully understand btw).
motor = gameObject.GetComponent(“CharacterMotor”); - I have the CharacterMotor.js script added to my character.
I would have thought this would allow the line to find the “canControl” boolean variable in the CharacterMotor script.
Here is the Update function. Please say if I should provide the entire script.
I’d really appreciate any answers on this, I’ve been trying to figure it out for a few hours and havent got anywhere.
function Update()
{
if(GameManager.pause || GameManager.scores)
{
moveDir = Vector3.zero;
motor.canControl = false;
}
else
{
GetUserInputs();
if(!motor.canControl)
{
motor.canControl = true;
}
if(!dead)
{
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
}
else
{
moveDir = Vector3.zero;
motor.canControl = false;
}
}
}