Hello, I am not good with scripts but I know how to use them. I am trying to make a 3rd person shooter so I put this script on my character and I placed a camera and I get this error when I try to play it.
“NullReferenceExpectation: Object reference not set to an instance of an object soldier script.update () ( at Assets/ soldier script.js : 13”
I used this script
}var speed = 3.0;
var rotateSpeed = 3.0;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
}
@script RequireComponent(CharacterController)
Thanks in advance
What line is the error on? It says line 13 but what line is that?
My only guess would be that its because you are setting the variable controller every frame ( because its in the update function). Try putting that line into the start function
function Start()
{
var controller : CharacterController = GetComponent(CharacterController);
}
Here is the lines numbered.
- #pragma strict
- function Start () {
- var speed = 3.0;
- var rotateSpeed = 3.0;
- function Update () {
- var controller : CharacterController = GetComponent(CharacterController);
- transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed, 0);
- var forward = transform.TransformDirection(Vector3.for ward);
- var curSpeed = speed * Input.GetAxis (“Vertical”);
- controller.SimpleMove(forward * curSpeed);
- }
- @script RequireComponent(CharacterControlle
It’s easier to read the code if you use code tags like this:
#pragma strict
function Start () {
var speed = 3.0;
var rotateSpeed = 3.0;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.for ward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
}
@script RequireComponent(CharacterControlle
As you can see, this is much easier to read and find problems. I’ll assume the ‘Vector3.for ward’ was a typing mistake when you posted the code, otherwise you probably would’ve mentioned an error about it. It looks like your real problem is that there is no closing squiqqly bracket (sorry, I don’t know the technical term) at the end of the Start() function. Also, I’m not sure why you’re declaring those variables in the start function. If you declare them outside of any function, you can set/change the values in the Inspector window (even while running the game).