-4

How can I get model description list (like Neck,Spine,lower lip etc) whatever is defined for a 3d character model ? I tried to write a script which is:

using UnityEngine; using System.Collections; public class Arrays : MonoBehaviour { public GameObject[] players; void Start() { players = GameObject.FindGameObjectsWithTag("avatar_5"); for (int i = 0; i < players.Length; i++) { Debug.Log(players[i].name); Debug.Log("Player Number " + i + " is named " + players[i].name); } } } 

but in result I am getting this error: UnityException: Tag: avatar_5 is not defined

3
  • You're looking for a Tag with FindGameObjectsWithTag, not the Name of your object. Has your "avatar_5" the right Tag? (this is not the name). Commented Jan 23, 2017 at 8:36
  • so should i tried like GameObject.Find("avatar_5") Commented Jan 23, 2017 at 8:40
  • this will return only one GameObject. Mostly the first one with the exact name in the list. Commented Jan 23, 2017 at 8:41

1 Answer 1

13

Understanding

First of all, what you describe as model description are simple GameObjects in Unity3D. And your exception tells us that your GameObject hasn't the right tag on it.

Also there is a huge difference between an GameObject's Name and his Tag.

Can you see the difference?

So if you want to print all children of a specific GameObject you have to find it first and then access it's child with GameObject.GetChild().

As the comments mentioned, GameObject.Find() will only return the first GameObject with the exact name, not all. So we have to loop through all GO's for finding the one with the right name.


Solving process

To accomplish your question, I guess we have to print the hierarchy of GameObject's. So we simply check all GameObject's if there are parent Objects, and collect them in a list. Then we can loop through them and read their children.

For checking if a GameObject is a parent or has a Child we always have to look at the Transform Component of the given GameObject.

Be aware of that, this kind of looping is very performance intensiv task.

Here is some example code, for a better understanding of what I mean and how this behavior in Unity3D works:

using UnityEngine; using System.Collections; using System.Collections.Generic; public class ReadAllGOs : MonoBehaviour { void Start() { var parents = FindParentGameObjects(); for (int i = 0; i < parents.Count; i++) { Debug.Log("--> Parent Number " + i + " is named: " + parents[i].name); ReadChildren(parents[i]); } } List<GameObject> FindParentGameObjects() { List<GameObject> goList = new List<GameObject>(); foreach (GameObject go in GameObject.FindObjectsOfType(typeof(GameObject))) { if (go.transform.parent == null) { goList.Add(go); } } return goList; } void ReadChildren(GameObject parent) { for (int i = 0; i < parent.transform.childCount; i++) { GameObject child = parent.transform.GetChild(i).gameObject; Debug.Log(string.Format("{0} has Child: {1}", parent.name, child.name)); // inner Loop ReadChildren(child); } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.