- Home /
Delete first Element on Array (JS)
Hey Guys! For many people its a cheap question but i dont know how to handle this:
I created a Trigger which spawns Obstacles if the player goes trough - after he enters another one this trigger stop spawning new Obstacles. So i put all the new spawned Obstacles into an array, because i want to delete them after X time (because of memory overload). The Question is:
How can i delete the first Obstacle which is on the first spot in the array (i hope iam right if i think its the first and oldest Obstacle i spawned)?
var spawnerHotSpot:Transform;
var spawnObject:GameObject;
var arraySpawnedObjects:Array;
var deleteInterval:int;
private var deleteTimer = 0.0;
var spawnInterval:int;
private var spawnTimer = 0.0;
public static var spawnActive:boolean = false;
function Start ()
{
}
function Update ()
{
spawnTimer += Time.deltaTime;
arraySpawnedObjects = GameObject.FindGameObjectsWithTag("Obstacle");
if(spawnActive == true)
{
if(spawnTimer >= spawnInterval)
spawnObstacle();
}
for(var dO : int = 0; dO < arraySpawnedObjects.length; dO++)
{
if(deleteTimer >= deleteInterval)
{
arraySpawnedObjects.remove(arraySpawnedObjects[dO]);
}
}
}
function spawnObstacle ()
{
Instantiate(spawnObject, spawnerHotSpot.position, spawnerHotSpot.rotation);
spawnTimer = 0.0;
}
(there are two other scipts but they arent necessary for this question - so pls ignore some variables which are not instanced in this part like spawnActive.)
Thank you for you answers!
Rather than arrays you can use List which is better to handle and also efficient than JS arrays.
Answer by robertbu · Jul 03, 2014 at 04:03 PM
First, you are using the Array class. It is untyped and slow, and not recommended. But if you are using it, there is a RemoveAt() function in the class. Note this removes the entry from the array. It does not destroy the game object (which is what I believe you want). GameObject.FindGameObjectsWithTag() returns a built-in array. I don't see any reason in this code to make 'arraySpawnObject' a class instance variable. It can be local to the function. So you can change line 25 to:
var arraySpawnObjects = GameObject.FindGameObjectsWithTag("Obstacle");
Or:
var arraySpawnObjects : GameObject[] = GameObject.FindGameObjectsWithTag("Obstacle"); ;
No Remove() function exists for the built-in array class. It is possible to copy all the elements down and resize a built-in array but it is not what you want here. I believe all you want to do is:
if(deleteTimer >= deleteInterval)
{
Destroy(arraySpawnedObjects[0]);
}
Note that is likely that the first element is the oldest, but I don't believe anything in the API references guarantees the order. And if it the order is correct now, it is possible that it will change in some future version of Unity. And you don't need to worry about deleting the array entry because 1) it is generated anew each frame, and 2) it is only a reference to the game object and therefore very small.
Your answer
Follow this Question
Related Questions
Wrong object always Instantiated 1 Answer
How to make an array list set to false gradually instead of instantly? 2 Answers
I want to add cloned gameobjects that are triggered, to an array. 2 Answers
Copy Childen Of GameObjects From Array 1 To Array 2 1 Answer
Array along on Z axis and along X axis. 0 Answers