- Home /
array question
so i need to remove an element of an array NO LIST!!!
so, i tried use removeAt but it gives me an error and doesn't remove the element: missing method expectation
here's the code:
var waypoints : GameObject[];
var closestWaypoint : GameObject;
@HideInInspector
var distance = Mathf.Infinity;
var moveTowardWaypoints : boolean;
var speed : float;
var maxSpeed : float = 2;
var horizontalMovement : Vector2;
var lastWaypoint : GameObject;
var lookForNewWaypoint : boolean;
function Update()
{
transform.LookAt(closestWaypoint.transform);
transform.rotation.x = 0;
transform.rotation.z = 0;
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalMovement.magnitude > maxSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
}
function LateUpdate()
{
if (lookForNewWaypoint)
{
for (var go : GameObject in waypoints)
{
var diff = (go.transform.position - transform.position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closestWaypoint = go;
distance = curDistance;
lookForNewWaypoint = false;
}
}
}
if (lastWaypoint)
for(var i : int = 0; i < waypoints.Length; i++)
{
if(waypoints[i].gameObject == lastWaypoint)
{
waypoints.RemoveAt(i);
}
}
}
function FixedUpdate()
{
rigidbody.AddRelativeForce(0,0,1 * speed);
}
function OnTriggerEnter(col : Collider)
{
if (col.transform == closestWaypoint.transform)
{
if (!lastWaypoint)
{
lastWaypoint = col.gameObject;
lookForNewWaypoint = true;
}
}
}
Answer by robertbu · Feb 23, 2014 at 05:46 PM
You cannot removed items from a built-in array. So your choices are:
Convert your code to use something like a List that has a RemoveAt() and use that in your code.
Convert back and forth between a built-in array and some array that has RemoveAt (link below).
Write your own RemoveAt(). You could copy all the elements above the removal point down one and then use System.Array.Resize() to change the size of the array.
http://answers.unity3d.com/questions/17033/how-do-you-use-removeat-or-add-with-an-array-of-ga.html
i felt sooooo stupid reading this!(i actually new this from another project) thank you so much for the help!
Answer by YoungDeveloper · Feb 23, 2014 at 05:56 PM
@robertbu pretty much explained it all. But consider that array copying or "resizing", which is copying too, is an expensive process, especially if arrays are large. So i suggestion is if you have array of max waypoints, lets say 50, and any action in the future will be between 0 and 49 waypoints, there's really no need to resize the array after each removed of added element just to save that tiny tiny amount of ram, you will be doing a ton more calculations. You can always set gameobject element to null, and consider it as empty.
Your answer

Follow this Question
Related Questions
How to remove an object from an array once it has been destroyed 1 Answer
identify and remove an item from a list 1 Answer
problem adding element of array 1 Answer
Remove object from Array in javascript 1 Answer
Remove element from array 3 Answers