Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Finding gameObjects and adding to a list

Discussion in 'Scripting' started by JakeBilbe, Nov 9, 2015.

  1. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    A while ago I posted up a thread about Random Map Generation, I'd created something I liked but it was very basic so I've decided to go over and re-code the whole thing. I need to be able to access GameObjects easily and to reduce lag I decided to add them to a List instead of searching each time.

    Initially I'm running this to test with before cleaning it up, but the issue I'm having is there should be two gameObjects with the Script "Connector" that has a boolean set to false called isUsed. But it's only finding and listing one of them, before this is called I instantiate the first room that only has one connector. Could this be why? I thought setting a List to a new List<> reset the size of it?

    Code (csharp):
    1.  
    2. GameObject[] foundConnectors = GameObject.FindGameObjectsWithTag("Connector");
    3.         foreach(GameObject c in foundConnectors) {
    4.             Connectors = new List<GameObject> { };
    5.             Connectors.Add(c);
    6.         }
    7.  
    8. foreach(GameObject c in Connectors) {
    9.             if(c.GetComponent<Connector>().isUsed) {
    10.                 Connectors.Remove(c);
    11.             }
    12.         }
    13.  
    EDIT: Nevermind, as soon as I posted the code I realized what I'd messed up with so just in-case anyone has a similar issue take a look at the code below, it's essentially the same as above.

    Code (csharp):
    1.  
    2. GameObject[] foundConnectors = GameObject.FindGameObjectsWithTag("Connector");
    3.         Connectors = new List<GameObject> { };
    4.  
    5.         foreach(GameObject c in foundConnectors) {
    6.             if(!c.GetComponent<Connector>().isUsed) {
    7.                 Connectors.Add(c);
    8.             }
    9.         }
    10.  
     
  2. Mr_Teels

    Mr_Teels

    Joined:
    Jul 14, 2015
    Posts:
    14
    i think the problem is that both Objects have the Name "Something(Clone)". evt. Try to Rename the Objekts when they are spawning. Something1 ... Something 2 ...
     
  3. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    Thanks for the reply, it wasn't that as I was using Tags the issue was that I was running List = new for each time I ran the Add command, so I was always ending up with one GameObject. I just had to place this above the Foreach statement and clean the code up.
     
    Mr_Teels likes this.