Getting a prefab to follow waypoints

Hi guys,

I’m using a NavMesh on a prefab and hes walking around following the waypoints perfectly, but only when hes placed in the scene, if he is instantiated the waypoints are not in the slots in the inspector. I cant work out how to get the waypoints into the prefab so he will follow them on instantiation.

You cant drag the waypoints into the prefab either, only if its already in the scene. I cant have the waypoints in the prefab either or they move with him and he will never reach them, so im pretty stumped.

here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class WaypointManager : MonoBehaviour
{

    NavMeshAgent agent;
    public Transform[] waypoints;
    int waypointIndex;
    Vector3 target;

    void Start()
    {
       
        agent = GetComponent<NavMeshAgent>();
        UpdateDestination();
    }

    void Update()
    {
        print(Vector3.Distance(transform.position, target));

        if (Vector3.Distance(transform.position, target) < 3f)
        {
            IterateWaypointIndex();
            UpdateDestination();
        }
    }

    void UpdateDestination()
    {
        target = waypoints[waypointIndex].position;
        agent.SetDestination(target);
    }

    void IterateWaypointIndex()
    {
        waypointIndex++;
        if (waypointIndex == waypoints.Length)
        {
            waypointIndex = 0;
        }
    }
}

Your prefab can’t connect to stuff in the scene. When you instantiate it, it has no drag and drop connections to stuff in the scene. So, instead of doing that, you’ll have to have a script in the scene that holds your references for you. Something like a waypoint manager that you can reference through a static variable.