0

EDIT: This is different from other questions because the code is much more specific to NavMesh code. I haven't found any questions specific to my code. (also other question seems to involve null?)

I'm trying to learn NavMeshAgent for use in point and click movement for a game using RTS style movement. I keep getting errors. The first issue is "Load Failed: Object reference not set to an instance of an object" - my code is below.

using UnityEngine;
using System.Collections;

public class PlayerNavigation : MonoBehaviour {

    public Camera cam;
    public NavMeshAgent navMeshAgent;

    void Start () { 
        cam = Camera.main;
        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    void Update () {
        if(Input.GetKeyDown  (KeyCode.Mouse0)){
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;

            if(Physics.Raycast( ray, out hit)){
                navMeshAgent.SetDestination ( hit.point );
        }
    }
 }
}

My next issue, seems to be compiling error. I'm hoping fixing the above code will fix it. When I press play, it says "All compiler errors have to be fixed before you can enter playmode"

Any help would be greatly appreciated.

  • Edited to show why it is not an exact duplicate – James Johnson Apr 01 '18 at 16:45
  • Solved myself thanks. – James Johnson Apr 01 '18 at 18:18
  • Even if your question isn't an exact duplicate based on the information you provided in your question it seems like a duplicate, you didn't give us any idea where the error message in your code was. `"Load Failed: Object reference not set to an instance of an object"` If this was the only error, what was the line number? In regards to your comment, if an object reference is not set, that typically means its null hence why you got marked as a duplicate for NullReferenceException. Regardless if your code is about NavMeshes the solution to a null reference is essentially the same. – AresCaelum Apr 02 '18 at 13:17
  • Thanks. I am very new to coding and Unity, so I did not know enough to realize that. Thank you for the explanation. Even though I was not able to precisely answer my question, I did find a work-around. – James Johnson Apr 03 '18 at 01:04

0 Answers0