Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Null refrence exception for rpg game

ive been watching brackeys tutorial for rpg im on the first episode and when i click somewhere it says:NullReferenceException: Object reference not set to an instance of an object

ControllerPlayer.Update () (at Assets/ControllerPlayer.cs:27)

here are the scripts :

ControllerPlayer:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


[RequireComponent(typeof(MotorPlayer))]

public class ControllerPlayer : MonoBehaviour{


public LayerMask movementMask;


Camera cam;

PlayerMotor motor;

// Start is called before the first frame update

void Start(){

cam = Camera.main;

motor = GetComponent<PlayerMotor>();

}


// Update is called once per frame

void Update(){

if (Input.GetMouseButtonDown(0))

{

Ray ray = cam.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;


if (Physics.Raycast(ray, out hit, 100, movementMask))

{

motor.MoveToPoint(hit.point);

}

}

}

}

MotorPlayer :

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;


[RequireComponent(typeof(NavMeshAgent))]

public class MotorPlayer : MonoBehaviour{


public NavMeshAgent agent;


// Start is called before the first frame update

void Start(){

agent = GetComponent<NavMeshAgent>();

}


public void MoveToPoint (Vector3 point)

{

agent.SetDestination(point);

}

}

Answers

Sign In or Register to comment.