- Home /
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! :/
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();
}
}
Answer by Robinmtb · Jan 15, 2012 at 12:35 AM
Hi!
This should do the trick :)
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
Where do I place everything? I keep getting errors:
"CS0246: The type or namespace name List
1' could not be found. Are you missing a using directive or an assembly reference?"
You need the System.Collections.Generic
namespace where the generic List<>
is defined in by adding
using System.Collections.Generic;
at the top of your script.
Alternatively you can use the full classname:
System.Collections.Generic.List<GameObject> temp = new System.Collections.Generic.List<GameObject>(Waypoints);
which is not really a good thing ;)
Answer by aldonaletto · Jan 15, 2012 at 04:50 AM
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; } }
Both of them worked great! Thanks aldonaletto for writing it out!
Don't post comments as answers. Rather comment on any answers that helped you. Please also 'mark as answer' the post that helped you most so we know your question is resolved.
Your answer
Follow this Question
Related Questions
Gather all GameObjects with a certain name in a Scene and Apply them to a list? 3 Answers
Moving a GameObject (camera) forward and backward along a series of points (C#). 1 Answer
How can I detecting whether certain characters appear in any strings in my array of strings? 1 Answer
How do I make an array and then access an Object from the array 2 Answers
Question on for loop with array 0 Answers