Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Requesting array help

Discussion in 'Scripting' started by chitanblue, Aug 23, 2013.

  1. chitanblue

    chitanblue

    Joined:
    Mar 15, 2013
    Posts:
    8
    My goal is to spawn three lunch bags around a map. I have a gameobject, called Lunch Spawn, that is parent to three Lunch Bag game objects.

    My goal is to store the lunch spawn's children into an array, so that when the lunch spawn instantiates, it will have an array of it's three children. I want to do this through code because I may have multiple lunch spawns throughout the map and I don't want to have to manually store each one in the inspector.

    How can I loop through a paren't children and store them into an array? Thanks

    This is what i was thinking so far
    Code (csharp):
    1.  
    2. var lunches : GameObject[];
    3. function Start()
    4. {  
    5.     for(var i:int = 0; i < transform.GetChildCount(); i ++)
    6.     {
    7. // don't know how to store the children.  
    8. //      lunches[i] = transform.GetChild ????
    9.     }
    10. }
    11.  
     
    Last edited: Aug 23, 2013
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    yes
    transform.GetChild(i) should return that child
     
  3. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Or as you spawn it add it to the array then... Usually works out good
     
  4. chitanblue

    chitanblue

    Joined:
    Mar 15, 2013
    Posts:
    8
    Thanks!!