Remove item from array

Hey, I have seen various questions on this but can only really find C# scripts which to me is no help. I have a script that instantiates enemies at run time at different spawnpoints (gameobjects). What I need is when and enemy is spawned at a gameobject, I need the gameobject to be destroyed (from the array) so that multiple enemies do not spawn on top of each other. Here is what I have:

var chicken:GameObject;
var spawn:Transform[];

function Start(){
var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
Instantiate(chicken,spawnpos.position, spawnpos.rotation);

}

// In a nutshell, if enemy spawns at spawnpoint1, delete spawnpoint 1 so enemy2 cannot spawn there. Any help/advice is appreciated guys :slight_smile:

There are all sorts of possible ways of doing this. An approach would be to do pretty much what you are already and then after each spawn event allocate a new, shorter array and copy results back into that. The overhead for that is unappealing though, so let’s do something else entirely. What this sample will do is leave the initial array intact and instead build the whole random sequence during Start.

#pragma strict
import System.Collections.Generic;

var prefab: GameObject;
var points : Transform[];

private var spawnIndex : int;
private var sequence : Transform[];

function Start () {
    var totalPoints : int = points.Length;
    sequence = new Transform[totalPoints];
    var indices : List.<int> = new List.<int>(totalPoints);
    // Stock the array with the whole set of possible points
    for (var i : int = 0; i < totalPoints; ++i){
        indices.Add(i);
    }
    // Now randomly select from this list, removing items from it and filling the sequence
    var random : int;
    var selectedIndex : int;
    var sequenceIndex : int;
    while ( totalPoints ){
        // Pick a point out of the set of indices
        random = Random.Range(0, totalPoints);
        selectedIndex = indices[random];    
        sequence[sequenceIndex] = points[selectedIndex];
        ++sequenceIndex;
        // Now we've made use of this index, prevent it from being selected again
        indices.RemoveAt(random);
        // Less points available, make note of that so the next random selection is valid and that we exit the loop correctly.
        --totalPoints;
    }
}

// Call this each time you want to spawn a new item.

function SpawnOne() {
    if (spawnIndex < sequence.Length){
        var at : Transform = sequence[spawnIndex];
        if (null != prefab){
            Instantiate(prefab, at.position, at.rotation);
        }else{
            Debug.LogError("No prefab to spawn");
        }
        ++spawnIndex;
    }else{
        Debug.Log("Spawn points are exhausted.");
    }
}

// Debug method - so you can see what is going on.

function OnGUI() {
    GUILayout.BeginArea(Rect(10f,10f,120f,120f));
    GUILayout.BeginVertical();

    for (var i : int = 0; i<sequence.Length; ++i){
        if (i == spawnIndex - 1){
            GUILayout.Label(String.Format("{0}] {1} <--- last spawn", i, sequence*.name));*

}else{
GUILayout.Label(String.Format(“{0}] {1}”, i, sequence*.name));*
}
}

GUILayout.EndVertical();
GUILayout.EndArea();
}

Use a List. C# answers will help you in most cases since both languages have most of the same stuff available; just translate to Unityscript syntax. (Unless you don’t know C# at all, but they’re actually more similar than they are different so it doesn’t take much effort.)

import System.Collections.Generic;

var chicken : GameObject;
var spawn : List.<Transform>;

function Start() {
    var itemNumber = Random.Range (0, spawn.Count);
    var spawnpos = spawn[itemNumber];
    spawn.RemoveAt (itemNumber);
    Instantiate (chicken, spawnpos.position, spawnpos.rotation);
}