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

I know this topic has been posted a lot in internet but i cant solve the solution

this is error that i get

this is my code on the playermovement

using System;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
using UnityEngine.AI;

[RequireComponent(typeof (ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
ThirdPersonCharacter thirdPersonCharacter; // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster = null;
Vector3 currentDestination, clickPoint;
AICharacterControl aiCharacterControl = null;
GameObject walkTarget = null;

//TODO solve fight between serialize and const
[SerializeField] const int walkableLayerNumber = 9;
[SerializeField] const int enemyLayerNumber = 10;

bool isInDirectMode = false;
private void Start()
    cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
    thirdPersonCharacter = GetComponent<ThirdPersonCharacter>();
    currentDestination = transform.position;
    aiCharacterControl = GetComponent<AICharacterControl>();
    walkTarget = new GameObject("walkTarget");

    cameraRaycaster.notifyMouseClickObservers += ProcessMouseClick;

void ProcessMouseClick(RaycastHit raycastHit, int layerHit)
    switch (layerHit)
        case enemyLayerNumber:
            //navigate to the enemy
            GameObject enemy = raycastHit.collider.gameObject;
        case walkableLayerNumber:
            //navigate to point on the ground
            walkTarget.transform.position = raycastHit.point;
            Debug.LogWarning("Don't know how to handle mouse click for player movement");


private void ProcessDirectMovement()
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");

    // calculate camera relative direction to move:
    Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
    Vector3 movement = v * cameraForward + h * Camera.main.transform.right;

    thirdPersonCharacter.Move(movement, false, false);


What are you clicking on when you get the error?

i click on the ground then i cant walk and this error pop out

What happens if you click on the enemy instead?

same error pop out also

enemyLayerNumber, walkableLayerNumber, raycastHit, and layerHit are values, so they don’t have references. The problem doesn’t seem to be enemy or walkTarget. So that leaves aiCharacterControl.

AICharacterControl should exist. It’s a required component. But maybe you should add a Debug.Log() check just to be sure.

which line should i add the Debug.Log()?

I would add Debug.Log(aiCharacterControl); after the aiCharacterControl in Start() and before the aiCharacterControl line in “case WalkableLayerNumber”.

Make a note as to whether you get one or two results.

Also, before playing, manually check player to make sure it has an AICharacterControl script and that the script isn’t missing a script file or anything.

is it should be like this and i still get the error

okay the error didnt pop out anymore but when i click the character doesnt move and when i click the enemy player also doesnt move

What changed?

Privacy & Terms