Search code examples
c#unity-game-engine

Unity, How do i use the Patroll.cs script i created ? Getting errors


In the Hierarchy I have a Main Camera, ThirdPersonController, Plane, Terrain, Cylinder, and Wall.

And in Assets I created a new folder called "My Scripts", and in there I created a new script in C# called Patroll.cs. The script should make the ThirdPersonController character walk between two given points to patrol. But when I'm adding/dragging the Patroll script to the ThirdPersonController, I'm getting an error.

The error is in the Patroll.cs script on the line:

if (agent.remainingDistance < 0.5f)

The error is:

MissingComponentException: There is no 'NavMeshAgent' attached to the "ThirdPersonController" game object, but a script is trying to access it. You probably need to add a NavMeshAgent to the game object "ThirdPersonController". Or your script needs to check if the component is attached before using it.

This is the Patroll.cs script code

using UnityEngine;
using System.Collections;

public class Patroll : MonoBehaviour {

    public Transform[] points;
    private int destPoint = 0;
    private NavMeshAgent agent;

    // Use this for initialization
    void Start () {

        agent = GetComponent<NavMeshAgent>();

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        GotoNextPoint();
    
    }
    
    void GotoNextPoint() {
        // Returns if no points have been set up
        if (points.Length == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }


    void Update () {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (agent.remainingDistance < 0.5f)
            GotoNextPoint();
    }
}

What I tried to do then is click in the Hierarchy on the ThirdPersonController, and then in the menu I clicked on Component > Navigation > Nav Mesh Agent

Now in the ThirdPersonController in the Inspector I can see the added Nav Mesh Agent.

Now when I'm running the game I'm getting the same error:

MissingComponentException: There is no 'NavMeshAgent' attached to the "ThirdPersonController" game object, but a script is trying to access it. You probably need to add a NavMeshAgent to the game object "ThirdPersonController". Or your script needs to check if the component is attached before using it.

I tried to click in the menu on Window > Navigation and then clicked on Bake, but it didn't solve it. The same error on same line in the Patroll.cs script persisted.

But I added a Nav Mesh Agent to the ThirdPersonController so why in the error it say it's not attached? And how do I make the Agent to be enabled?

In the ThirdPersonController after added to it the Patroll script in the Inspector I see in the Patroll part. I can add points change other values, but the properties "Dest Point" and "Agent" are gray can't be used.

In the Agent I see "Agent: None (Nav Mesh Agent)", but I can't click on it, it's gray and disabled.

This is a screenshot of the ThirdPersonController Inspector on the right the Patroll script and the Nav Mesh Agent.

Screenshot

In the Patroll.cs script I changed the variable agent to be public:

public NavMeshAgent agent;

Now the ThirdPersonController have the Nav Mesh Agent and the Patroll script in the Inspector, and I also added as agent in the patroll: ThirdPersonController (Nav Mesh Agent), but now I'm getting the error:

"GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:get_remainingDistance() Patroll:Update() (at Assets/My Scripts/Patroll.cs:41)

Line 41 in the patroll script is:

if (agent.remainingDistance < 0.5f)

Screenshot


Solution

  • You have not assigned a NavMeshAgent component containing object in your Patroll script component.

    enter image description here

    Agent variable field should be public or serializeable private.

    public NavMeshAgent Agent;