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*.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;*
    
  •  }*
    
  • }*
    }

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

@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.