NullReferenceException: Object reference not set to an instance of an object

Hello everyone,

i have been getting the error in the title and i dont know what to do. I was able to figure out that the script couldn’t find the CharacterController. But i dont know how to fix it. i have put the script off and on the player with the CharacterController but it doesn’t help.

this is the code im using

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public float speed = 2f;
    public float runSpeed = 6f;
    public float jumpSpeed = 6f;
    public float gravity = 20f;

    private Vector3 moveDirection = Vector3.zero;
    private int jumps;
    private CharacterController controller;


    void start()
    {
        controller = GetComponent<CharacterController> ();
    }

    void Update()
    {
        if (!controller) //Check for controller reference
        {
            Debug.LogError("Didn't find CharacterController");
        }

        if (controller.isGrounded) {
            moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
            moveDirection = transform.TransformDirection (moveDirection);
            moveDirection *= speed;

            if (Input.GetButtonDown ("Jump")) {
                moveDirection.y = jumpSpeed;
            }
            jumps = 0;

            if (Input.GetKey (KeyCode.LeftShift)) {
                moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
                moveDirection = transform.TransformDirection (moveDirection);
                moveDirection *= runSpeed;
            }
        }
        else {
            moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), moveDirection.y, Input.GetAxis ("Vertical"));
            moveDirection = transform.TransformDirection (moveDirection);
            moveDirection.x *= speed;
            moveDirection.z *= speed;
            moveDirection.x *= runSpeed;
            moveDirection.z *= runSpeed;

            if (Input.GetButtonDown ("Jump") && jumps < 1) {
                moveDirection.y = jumpSpeed;
                jumps++;
            }
        }

        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move (moveDirection * Time.deltaTime);

        }
    }

Copy and paste the actual error message. It includes additional information, in particular the line number on which the error occurred.

So, this is the same issue as another post. Remember Unity magic methods always start with a capital letter.

start should be Start

If it doesn’t, it doesn’t run when Start should be called.

And as commented in another post, methods should normally start with caps anyways

1 Like

Yep, since you wrote it “start” instead of “Start” it will not be called until you specifically call that method. This type of issue should be found within minutes though. All you had to do was add a Debug.Log statement in “start” next to where you GetComponent, hit the Play button, and you would have seen that “start” is never ever called. Mystery solved.

2 Likes

I’m sorry is should have seen it needed to be Start and not start. I ony use unity a few months now and am a bit of a slow learner. Whenever I can’t find the problem I will look on Google. If I can’t find an answer there I ask my question here. The debug. Log I use I found via Google but more than that I don’t know a thing about debug logs.

So after changeing the s to and S i got loads of errors. turned oit that the step offset was to high by default. so it wasent even the script that had something wrong in it other than the S.

Thanks for the help :slight_smile:

1 Like