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);
}
}
}

