Problems with Arrays

The problem isn’t the array actually, the problem is finding a solution to my troubles really. Let me try and explain what I’m trying to do. First, I’m setting variables.

public var objScout1:GameObject;
public var objScout2:GameObject;
public var objScout3:GameObject;

I also make a built-in array for gameobjects.

public var wave1:GameObject[];

and in the start function I add these gameobjects to the array.

function start(){
    wave1 = new GameObject[3];
    wave1[0] = objScout1;
    wave1[1] = objScout2;
    wave1[2] = objScout3;

Elsewhere I’m instantiating each of these by use of random number,

function selection(b){
//b = random number based on how many objects are in the array
    var enemy = Instantiate(wave1**, Vector3(transform.position.x,Random.Range(-   14,14),transform.position.z), transform.rotation);**

This function takes a random number, instantiates the object and then what I would like it to do after that is, remove that same object from the array. The problem is, which I just found out a minute ago- you cannot remove objects from a built-in array because these arrays have a fixed size.
I have looked around and I found another method in which you could add gameobjects to an arraylist, the problem with that for me was that the length of the array would always came back null. Is that normal? Can anyone possibly give me some insight? Thank you.

Yes, there is a distinct difference between built-in arrays such as GameObject and Array(). If you want to resize a built-in array, you’ll have to handle the removal of the element yourself using a custom method to rebuild the array. Here’s code that should do the trick.

function RemoveItemFromArray(i : int) {
    var newCount : int = wave1.length - 1;
    if(!newCount) {
        wave1 = null;
    else {
        var newWave : GameObject[] = new GameObject[newCount];
        var y : int = 0;
        for(var x : int = 0; x < wave1.length; x++) {
            if(x != i) {
            // Skip the element at index i
                newWave[y] = wave1[x];
        wave1 = newWave;

This is what lists are for. They are type safe, but at the same time, they are resizable. There are a couple different types of lists, but in your case, the basic list should work well:

import System.Collections.Generic;

public var objScout1:GameObject;
public var objScout2:GameObject;
public var objScout3:GameObject;

public var wave1 = new List.<GameObject>();
//The GameObject between the brackets determines the type the list holds.

function Start () {


then you shouldn’t have to change how your instantiate code works. You can access lists the same way as arrays with the index i.e. wave1[0].

And its easy to remove items:


as described here.