Problem with navigate to Enemy and ground

I hope someone can help me with this, get this error message in Unity 2017 when i trying to navigate to enemy and on the ground so i can’t move at all.

“NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.ProcessMouseClick (RaycastHit raycastHit, Int32 layerHit) (at Assets/Player/PlayerMovement.cs:47)
CameraRaycaster.Update () (at Assets/Camera & UI/CameraRaycaster.cs:49)”

and

"NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.ProcessMouseClick (RaycastHit raycastHit, Int32 layerHit) (at Assets/Player/PlayerMovement.cs:42)
CameraRaycaster.Update () (at Assets/Camera & UI/CameraRaycaster.cs:49)
"
if i click on the problem it says it something wrong with this code in Playermovement.cs:

aiCharacterControl.SetTarget(enemy.transform); and aiCharacterControl.SetTarget(walkTarget.transform);

but could it be something with the layer in main camera? thankful for help.
/Anton

Hi Anton,

For the sake of anyone else who may run into the same/similar issue, what did you do to resolve your issue? (glad you resolved it btw :slight_smile: )

I’m getting a similar issue. I tracked it down to the lines in Camera Raycaster that say:

	// Notify delegates of highest priority game object under mouse when clicked
	if (Input.GetMouseButton (0))
	{
		notifyMouseClickObservers (priorityHit.Value, layerHit);
	}

same issue…

did some testing and i’m finding now that actually neither my Start() or Update() methods on my player are actually even running.

why oh why did you guys include a bunch of **** that wasn’t needed in that unity package. so we can practice not imporint stuff we don’t need?

kinda screwed over the lot of us who prepare for the lecture by pre-importing it so we don’t have to panic stop the lecture in the middle when you guys do it.

ok i remade the entire prefab, still getting the click error. decided to just disable click to move in my game.

I’m sorry, is this addressed to me?

no?

I see. It’s just that you replied to my post, so I assumed you were.

Hey, same issue here.

GameObject enemy = raycastHit.collider.gameObject;
aICharacterControl.SetTarget(enemy.transform);

is where it failes.

I am not using the 3rd person character as the enemy but just a gameobject with a spidermesh. Dont know if thats why its not working.

Also tried to check for null

GameObject enemy = raycastHit.collider.gameObject;
if (enemy != null) {
    aICharacterControl.SetTarget(enemy.transform);
}

But it is still complaining.

One solution is to use

agent.SetDestimation(raycastHit.point);

(Declaring Agent agent in the class)

This can be used for the walking also,
we would not need to make an empty game object and move that around.

However, im not sure what would happen if the enemy would be moving, we would walk to the destination but not track him.

I had the same problem and now its working. I’ll put copy my script here for you maybe works.

[SerializeField] float walkMoveStopRadius = 0.2f;
[SerializeField] float attackMoveStopRadius = 5.0f;

[SerializeField] const int walkableLayerNumber = 8;
[SerializeField] const int enemyLayerNumber = 9;

ThirdPersonCharacter thirdPersonCharacter = null;   // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster = null;
AICharacterControl aiCharacterControl = null;
GameObject walkTarget = null;

ThirdPersonCharacter Character = null;   // A reference to the ThirdPersonCharacter on the object
Vector3 currentDestination, clickPoint;

bool isInDirectMode = false;

private void Start()
{
    cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
    Character = 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:
            GameObject enemy = raycastHit.collider.gameObject;
            aiCharacterControl.SetTarget(enemy.transform);
            break;
        case walkableLayerNumber:
            walkTarget.transform.position = raycastHit.point;
            aiCharacterControl.SetTarget(walkTarget.transform);
            break;
        default:
            Debug.LogWarning("Don't know how to handle mouse click or movement");
            return;
    }

}

Privacy & Terms