x


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[b], Vector3(transform.position.x,Random.Range(-   14,14),transform.position.z), transform.rotation);
    wave1.RemoveAt(b);
}

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.

more ▼

asked Sep 20 '11 at 11:00 PM

feneq gravatar image

feneq
85 2 2 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 () {

    wave1.Add(objScout1);
    wave1.Add(objScout2);
    wave1.Add(objScout3);

}

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:

 wave1.RemoveAt(i);

as described here.

more ▼

answered Sep 21 '11 at 12:13 AM

Peter G gravatar image

Peter G
15k 16 44 136

wonderful, thank you !

Sep 21 '11 at 12:38 AM feneq
(comments are locked)
10|3000 characters needed characters left

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];
                y++;
            }
        }
        wave1 = newWave;
    }
}
more ▼

answered Sep 20 '11 at 11:53 PM

Steven Walker gravatar image

Steven Walker
862 25 32 48

This is great and works. It's great because something like this did not even occur to me so it will help me down the road when finding solutions to a problem. However, I'm not entirely sure, but I believe the answer posted by Peter G. may be better suited for the task at hand. Thank you sir.

Sep 21 '11 at 12:38 AM feneq
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2086
x1360
x355
x72
x9

asked: Sep 20 '11 at 11:00 PM

Seen: 754 times

Last Updated: Sep 21 '11 at 12:38 AM