Search Unity

How to automatically hook in game objects to prefab?

Discussion in 'Scripting' started by chrilley, Aug 15, 2014.

  1. chrilley

    chrilley

    Joined:
    Mar 15, 2013
    Posts:
    9
    I have a character model where I segment the character into different parts. I do this so I can easily change heads, hands, legs and add hats, vests, etc.
    I put this model in a prefab were I also have a little script attached. In this script I can hook my meshes in. It's very simple and straightforward, since I am not really a coder(yet? dun dun) I can't do much fancy scripting:

    public GameObject Head01;
    public GameObject Arms01;
    public GameObject Legs01;
    ...

    After this point it's up to the coders to do whatever they want with these objects.

    My problem is that I now have so many objects to hook in per prefab that this process takes ages whenever I have to update the character.

    So my question is basically; Is it possible to somehow in an editor script loop through all these game objects, look for a name match inside the .fbx and automatically hook it in for me?

    Or maybe even an alternative solution?
     
  2. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    If you use a naming convention you can put then directly under the root object and look up the children by name in the Awake or Start method.
     
  3. chrilley

    chrilley

    Joined:
    Mar 15, 2013
    Posts:
    9
    Thanks for the reply! Yeah, I'd like something like that. Are there any examples/documentation I could look at? I'm still green when it comes to coding.
     
    Last edited: Aug 18, 2014
  4. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
  5. chrilley

    chrilley

    Joined:
    Mar 15, 2013
    Posts:
    9
    Thanks!

    A follow-up question, what if there are multiple instances of this character prefab with the same naming? Would it then grab the "Hand" object from a totally different instance than its own? If so, how do I prevent this from happening?