x


How would I find the name of all game objects in a c# List

Hi I am trying to figure out the best way to find all instantiated bombs (GameObjects) so far the way I have theorised (please tell me if there is a more efficient method of doing this) is to add all the bombs to a static array/List on instantiation called bombList.

When the bomb has exploded I will remove it from the list. The problem comes now when I want to access all the bombs at once, I found the foreach method and I was wondering if there was a way of using it to say access all the bombs names?

I realise that i could make a function which uses a for loop to iterate through the list and return the names but I was just wondering (for the sake of expedience and knowing) if there is a more efficient/ implemented way of doing this. As it stands I probably have to do this sort of process for a lot of things so any advice on better ways of dealing with this is also appreciated.

// New List for storing Bomb Objects
public static List<GameObject> bombList = new List<GameObjects>();

void Start () {
    bombList.Add(this.gameObject);
}
void Update(){
    if(Input.GetButtonDown("Jump"){
        // get all of the bombs currently in bombList and print their names out
    }
}
more ▼

asked Aug 09 '11 at 03:12 PM

sacredgeometry gravatar image

sacredgeometry
203 11 15 26

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

1 answer: sort voted first

I would just use a foreach as you are using a List.

   foreach(var go in bombList)
    {
        Debug.Log(go.name);
    }
more ▼

answered Aug 09 '11 at 03:44 PM

RoflHarris gravatar image

RoflHarris
971 5 8 13

Yeah that is what I thought would be the answer I will wait to see if anyone can address whether or not there is an alternative if not ill accept that this is the best way. I was just wondering how expensive it is to call this every frame as thats what I would need to do.

Aug 09 '11 at 03:47 PM sacredgeometry
(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:

x4166
x2090
x1363
x356
x63

asked: Aug 09 '11 at 03:12 PM

Seen: 982 times

Last Updated: Aug 09 '11 at 03:50 PM