Search Unity

Find GameObjects and put them in an array

Discussion in 'Scripting' started by blackdragonstory, Apr 25, 2015.

  1. blackdragonstory

    blackdragonstory

    Joined:
    Nov 21, 2010
    Posts:
    99
    Hi
    I have a empty object that is a parent to many different objects. Basicly imagine 10 swords,10 shields... I want to create a script that would always load whatever gameObject is a child of the first empty object akka parent. So if I want to add another sword I can just place the model under the parent and when I run the game it loads the new sword model instantly. The gameObjects would be stored in a array.

    I tried various ways,but couldnt figure it out. The closest I got was storing 1 GameObject in the array.

    I use java,so please post answers in java as well. Tnx
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    To loop through the children of a GameObject, just a foreach loop:
    Code (csharp):
    1.  
    2. foreach(Transform child in gameObject.transform)
    3. {
    4.      Debug.Log(child.name);
    5. }
    6.  
    So you could build your array or list from that.

    I'm not entirely sure what you're trying to do -- your phrasing is a little confusing, possibly due to a language barrier -- so if you try to clarify further I'd be happy to help more.
     
  3. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    Tag your GameObject you want to be your parent and put all item as his childs, then tag the main Gameobject that is your Player
    GameObject player =GameObject.FindGameObjectWithTag("Player");
    GameObject parent = GameObject.FindGameObjectWithTag("GAMEOBJECTTAG");
    parent.transform.GetChild(NUMBEROFCHILDINHIERARCHY-1).transform.parent = player.transform;

    I think that is what you want
     
  4. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    Tell me in your player do you have only Weapon child or you have more than one?
     
  5. blackdragonstory

    blackdragonstory

    Joined:
    Nov 21, 2010
    Posts:
    99
    I am acutally have 4 different parts.
    For example:
    Swords parent has many different sword child models under it.
    Shiedl parent has many different shield child models under it.
    ...
    If I can establish how to get sword child models I will be able to do the others as well since they are the same.
    I wanted to avoid tags.(can one object have more than one tag? for example tag player & sword)

    What I want is that this script does everything automaticly so that I dont have to worry.about it later.
    So that I can just add new models in game and the script automaticly loads the model and later automaticly ads it in menu where people can select it.

    Btw,how would I load stats of the model from a txt file?
    For example when I run the game the txt file is created and all the loaded models names are written on that txt file and then I just edit the file and add stats next to each name.

    Hope it's a bit clearer now ;)
     
    Last edited: Apr 27, 2015