C# Array problems

I create an array of GameObjects called WayPoints and then find the closest one, but then I can’t seem to understand how to delete the waypoint from the Array once my cube reaches it?

HELP! :confused:


public GameObject WayPoints;

GameObject FindWaypoint()
	{
    	WayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
		
        foreach (GameObject item in WayPoints) 
		{
            Vector3 diff = item.transform.position - position;
            float curDistance = diff.sqrMagnitude;
			
            if (curDistance < distance) 
			{
                closest = item;
                distance = curDistance;
            }
        }
        return closest;
    }

void Awake () 
{
WayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
}

void Update()
{
if(Vector3.Distance(gameObject.transform.position,TargetTile.transform.position) > 0.1)
		{
			transform.position = Vector3.Lerp(transform.position, TargetTile.transform.position, (Time.time - startTime) / duration);
		}
		else {
			TargetTile = FindWaypoint();
		}
}

Hi!

This should do the trick :slight_smile:

int WaypointIndex = Array.IndexOf(Waypoints, TargetTile); //Find index of reached waypoint
List<GameObject> temp = new List<GameObject>(Waypoints); //Duplicate old Array into a List
temp.RemoveAt(WaypointIndex); //Remove reached waypoint from templist
Waypoints = temp.ToArray(); //Update array

WayPoints is a builtin array, and you can’t remove elements from it easily - you must create a temp array with one less element, copy to it all waypoints but the one you want to delete and assign the temp array to WayPoints.

I made a few changes in your script to include this delete function, and reload the waypoints when needed. I also changed Lerp to MoveTowards and defined a speed variable instead of duration: this is a better alternative when you want to move at constant speed.

public float duration = 10;
public float speed = 2.5f;
public GameObject[] WayPoints;
private GameObject TargetTile;

void DeleteWaypoint(GameObject waypoint)
{
    GameObject[] temp = new GameObject[WayPoints.Length-1];
    int i = 0;
    foreach (GameObject elem in WayPoints){
        if (elem != waypoint) temp[i++] = elem;
    }
    WayPoints = temp;
}
	
GameObject FindWaypoint()
{
    if (WayPoints.Length == 0){ // if no waypoints, find them
        WayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
    }
    GameObject closest = null;
    float distance = Mathf.Infinity;
    Vector3 position = transform.position;
    foreach (GameObject item in WayPoints) 
    {
        Vector3 diff = item.transform.position - position;
        float curDistance = diff.sqrMagnitude;
        if (curDistance < distance) 
        {
            closest = item;
            distance = curDistance;
        }
    }
    DeleteWaypoint(closest); // delete this waypoint from the list
    return closest;
}

void Update()
{
    if (TargetTile && Vector3.Distance(transform.position, TargetTile.transform.position) > 0.1)
    {
        transform.position = Vector3.MoveTowards(transform.position, TargetTile.transform.position, Time.deltaTime * speed);
    }
    else {
        TargetTile = FindWaypoint(); // get next waypoint;
    }
}