Help making a Static Array

Hi, I have very limited experience coding and seem to need some help with some basics. I’ve been piecing together tutorials in an attempt to make a basic tower defense game. I have the movement finally working properly but when I instantiate the object the waypoints need to be manually assigned using my current script. I’ve tried to look around for my specific problem and it seems like the solution would be to make a Static that is always loaded in the level that the instantiated prefabs will automatically reference. Would someone mind helping me in the right direction based on my current code? Thank you so much for reading this and for any help!

The Waypoints Script

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

public class Waypoints : MonoBehaviour
{
    private void OnDrawGizmos ()
    {
        foreach(Transform t in transform)
        {

      
        Gizmos.color = Color.blue;
        Gizmos.DrawWireSphere(t.position, 1f);
        }

        for(int i = 0; i < transform.childCount - 1; i++)
        {
            Gizmos.DrawLine(transform.GetChild(i).position, transform.GetChild(i + 1).position);
        }
    }

    public Transform GetNextWaypoint(Transform currentWaypoint)
    {
        if(currentWaypoint == null)
        {
            return transform.GetChild(0);
        }
        if (currentWaypoint.GetSiblingIndex() < transform.childCount - 1);
        {
            return transform.GetChild(currentWaypoint.GetSiblingIndex() + 1);
        }
    }
}

The Movement Controller Script

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

public class Mover : MonoBehaviour
{
    [SerializeField] public Waypoints waypoints;
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float distanceThreshold = 0.1f;

    private Transform currentWaypoint;
    // Start is called before the first frame update
    void Start()
    {
   
        currentWaypoint = waypoints.GetNextWaypoint(currentWaypoint);
        transform.position = currentWaypoint.position;

        currentWaypoint = waypoints.GetNextWaypoint(currentWaypoint);
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, currentWaypoint.position, moveSpeed *Time.deltaTime);
        if (Vector3.Distance(transform.position, currentWaypoint.position) < distanceThreshold)
        {
            currentWaypoint = waypoints.GetNextWaypoint(currentWaypoint);
        }
       
}
}

It looks like your script automatically grabs all of the child transforms to use as waypoints, so currently it looks like you don’t have to assign them manually.
I don’t understand what your problem is or why a static array would help, or what you would use it for.

Sorry for the clarity. In the editor, the Serialized Field for Waypoints waypoints is how the target for the array is formed. So each enemy prefab is looking for that waypoint object to be targeted.
9406154--1316921--upload_2023-10-12_15-23-8.png
This is the inspector for the spawned enemy prefab I have set to make sure the system moves the object. It has me specify that waypoints variable for each set to the waypoints parent. When a new prefab is instantiated it does not have the object targeted to find the array from and will not let me save the prefab with this target set. I apologize if this is still unclear again as I said I’m very unfamiliar so my ability to communicate my issue is limited thank you for taking the time!

9406154--1316918--upload_2023-10-12_15-22-56.png

Make an empty GameObject. Put the waypoints object and each enemy as children of the empty object. Make a prefab of the empty object and then you’ll have a prefab of the whole thing and all of the references will be preserved.

How would I instantiate new enemies then? Wouldn’t that Instantiate the entire waypoint system? I attempted to try this and the enemy didn’t even retain its assigned target either. Making the waypoints script into a referencable variable and hard coding the enemy script to grab it wouldn’t be easier?

How do you instantiate new enemies now?

If you have a script that spawns enemies, you could have that script assign the waypoints object and target to the enemies.

I got the prefab to retain the target, I guess ill just spawn a new system for each

Why? Why not just assign the same system to each one? Nothing I have advised so far should result in duplicate instances of the waypoints system.

If im making a prefab that contains the waypoint parent how would I not ?

Its alright though the impact is negligible to make the 12 waypoints and It works so I’m going to run with it

To be clear, my suggestion was to put one waypoints object and all of the enemies in a single prefab. Each enemy having a reference to the same waypoints object.

If your plan was to instantiate additional enemies dynamically, then you didn’t mention that in your original post, but I mentioned you could simply have your enemy spawner have a reference to the waypoints and pass it along when it spawns each enemy.

…but whatever works. :stuck_out_tongue: