- Home /
Removing objects from GameObject array
I have a GameObject array. I'm trying to remove objects from it based on trigger. But the compiler recognises it as a GameObject and not an array. How do I convert the GameObject into an array? Here's the code:
var tileArray : GameObject[];
var speed : float = 5;
var x : float;
var z : float;
var tile : GameObject;
function Update () {
x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate(x, 0, z);
//Debug.Log("Position is " + transform.position.x + " " + transform.position.y + " "+ transform.position.z);
}
function OnTriggerEnter (other : Collider) {
//Debug.Log("Triggered! " + other.gameObject.transform.position);
tileArray += [other.gameObject];
}
function OnTriggerExit (other : Collider) {
for (var i = 0; i < tileArray.length; i++)
{
if (tileArray[i] == other)
tileArray.RemoveAt(i); // Problem part
}
}
It throws me the following error:
Assets/Scripts/CharacterMove.js(33,35): BCE0019: 'RemoveAt' is not a member of 'UnityEngine.GameObject[]'.
Thanks in advance.
Answer by 767_2 · Sep 25, 2014 at 01:20 PM
RemoveAt is in System.Collections namespace so add the library to your code
using System.Collections;
for java script use this
var arr = new Array ();
this is the same questions use the link
Answer by MrGuardianX · Sep 25, 2014 at 01:47 PM
Considering UnityScript is js-alike, the solution would be:
tileArray.splice(i, 1);
Your answer

Follow this Question
Related Questions
Destroy on Collision? 1 Answer
Track debug - Object dissapears after collision 0 Answers
Destroying a prefab on collision with a cube? 1 Answer
Getting info about hit Object (Javascript). 1 Answer
Unity 2D Colliders not Colliding 2 Answers