- Home /
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
     }
 }
Answer by tnetennba · Aug 09, 2011 at 03:44 PM
I would just use a foreach as you are using a List.
    foreach(var go in bombList)
     {
         Debug.Log(go.name);
     }
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.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                