2D Pathfinding Script Help

Alright, so I’m working on a 2D game, and I used the script below, which I just copy and pasted from some website, to make NPCs patrol between points randomly. And it worked perfectly for a while. And now, suddenly, it’s giving me a bunch of errors about how there’s no NavMeshAgent attached to the asset the script is on, and when I do put an agent on the asset, I get the error saying that the asset needs to be put on a NavMesh, which I have since realized you cannot do in 2D. I do not want a solution that’s just to use 3D with an orthographic camera. I much prefer just using 2D, and this script has worked in the past in 2D, but now it’s just not. If you need to know, I’m in 2020.3.19f1, and that never changed in the project. I also did not change anything drastic that could have caused this error to start, I only placed a few prefabs that had nothing to do with this.

    // Patrol.cs
    using UnityEngine;
    using UnityEngine.AI;
    using System.Collections;


    public class Patrol : MonoBehaviour {

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


        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.pathPending && agent.remainingDistance < 0.5f)
                GotoNextPoint();
        }
    }

I’m sorry you’ve had this issue. This is why we use source control.

To solve this issue, it may be necessary to go back to the original website and reimplement what they did, but this time pay attention to all the parts involved. Then you can reason about how your present project is different from the original web implementation.

Meanwhile, please consider using proper industrial-grade enterprise-qualified source control in order to guard and protect your hard-earned work.

Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

As far as configuring Unity to play nice with git, keep this in mind:

https://discussions.unity.com/t/736093/3

Here’s how I use git in one of my games, Jetpack Kurt:

https://discussions.unity.com/t/807568/3

Using fine-grained source control as you work to refine your engineering:

https://discussions.unity.com/t/826718/2

Share/Sharing source code between projects:

https://discussions.unity.com/t/719810/2

Setting up an appropriate .gitignore file for Unity3D:

https://discussions.unity.com/t/834885/5

Generally setting Unity up (includes above .gitignore concepts):

https://thoughtbot.com/blog/how-to-git-with-unity

It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place.

“Use source control or you will be really sad sooner or later.” - StarManta on the Unity3D forum boards

Problem solved: Apparently I had two different patrol scripts, which both did the exact same thing, except one didn’t use any NavMeshAgents. I have no idea where this second script came from, or how I ever ended up with two, but it happened.

I stand by my source control recommendation. It’s your project, value it accordingly.