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
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.
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.
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);
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments