Hey guys,
Does anyone know how I could check a parent for what children it has?
Thanks,
Dobe
Hey guys,
Does anyone know how I could check a parent for what children it has?
Thanks,
Dobe
its in the transform class.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.html
I didn’t see anything in the transform class that actually presented me with a list of the children a parent has. I did see GetComponentInChildren, but it doesn’t appear that that will handle my current issues.
Hope this helps: Your right too, i didn’t see this in the docs but i knew it existed otherwise:
public static GameObject Find(this GameObject go, string nameToFind, bool bSearchInChildren)
{
if (bSearchInChildren)
{
var transform = go.transform;
var childCount = transform.GetChildCount();
//Debug.LogError( go.name + " ChildCount: " + childCount);
for (int i = 0; i < childCount; ++i)
{
var child = transform.GetChild(i);
if (child.gameObject.name == nameToFind)
return child.gameObject;
GameObject result = child.gameObject.Find(nameToFind, bSearchInChildren);
if (result != null) return result;
}
return null;
}
else
{
return GameObject.Find(nameToFind);
}
}
That helps tremendously ![]()
Thank you for your time, it’s appreciated.
As a side note - Transform can be enumerated so that code can be shortened at the expense of the enumerator allocation.
foreach (Transform child in transform)
{
if (child.gameObject.name == nameToFind)
return child.gameObject;
}
Hey neato kelso, thanks ![]()
I’ve been playing around with this script and I’ve been getting a parsing error when I put it on a game object: “Assets/BG4/maps/RzTeset.cs(44,1): error CS8025: Parsing error”.
I am new to unity and C#, so I apologize if i’m missing something that’s glaringly obvious, but I really do appreciate your help/patience.
thanks,
Dobe.
see how your error has some numerics after the file name ‘RzTest.cs(44,1)’, that’s the line number where you’re having the issue. Go there, look at that line, that’ll give you an idea of what is actually causing the issue. There might be a syntactical issue there.
There’s not, the line is simply: " return GameObject.Find(nameToFind);". There appears to be something inherently wrong with the script.
The given example will return only first level children transforms, not children of children.
If you need to check all children you should use GetComponentInChildren().
I’m basically just testing this script on a simple setup. I have a cube who has 2 children, a cylinder and a sphere, in theory the script should return the names of those objects but it does not, I only get the parse error.
If you want to get all children and the children of their children you really should just do a recursive search.