Finding a gameobject child of a gameobject [SOLVED]

I cannot see anywhere in the API a method for finding a game object child of a game object (not a component). I tried using FindByTag, but it then finds the same object in another instance of the parent’s gameobject.

More detail:

I have two objects, a player and an enemy. They both have a child gameobject tagged “Right_Hand”. If I do FindByTag on the enemy it is finding the Right_Hand of the player…

Transform.Find().

–Eric

OK another one for you…

GameObject - ScriptA
|
|— GameObject - ScriptB

How can ScriptB “access” information (properties) in ScriptA? I cannot use SendMessageUpwards as ScriptA is just a data object…

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://unity3d.com/support/documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html

haha not sure why you sent me the second link.

Anyway, basically I cannot get the transform.Find method to work. It simply will not find the “hand” on the creature…

Because you asked

and global variables, addressed in the second link, are one way of going about that.

Post a code snippet, and preferably a small unitypackage or project, and perhaps we can help.

Actually I asked how one script can access another in a specific situation. I could not get a script from a child object to access the script on a parent object.

I understand global variables, but they would not work in this case.

The other way around works fine, because you just call GetComponentInChildren, but there is no GetComponentInParent.

The second issue I encountered was that the Find method on my models transform, would not Find the “hand”. If I hard code the path using

transform.Find("Bip01/Spine/R_arm/R_Hand");

it works, but I thought you could also do :

transform.Find("R_Hand");

but apparently not…

I don’t have any more advice about your first question then, without knowing more about your hierarchy.

As for the other…

Component[] boneComponents;
int numberOfBones;
Transform[] bones;
Transform r_Hand;

void SkeletonHijinx()
{
	boneComponents = GetComponentsInChildren(typeof(Transform) );
	numberOfBones = boneComponents.Length;
	bones = new Transform[numberOfBones];
	for (int i = 0; i < numberOfBones; ++i)
	{
		bones[i] = boneComponents[i] as Transform;
		r_Hand = bones[i].Find("R_Hand");
		if (r_Hand)
		{
			// Whatever, man
		}
	}
}

Thanks - that is a better way of doing it that I was (using a recursive method to search the whole hierarchy down from the main game object).

As for my first issue:

Below I have the prefab - spell_casting_bolts - it has one script attached “SpellInformation”. Now I have a script in casting_right_nuke which you can see in the inspector. It has a script called Hand Right Casting Spell, which needs to access SpellInformation. But I cannot in that script get a reference to SpellInformation (like I could the other way with GetComponentInChildren(…) .

Is there some reason you can’t use

transform.parent.GetComponent(typeof(SpellInformation) )

:?:

Or, declare the variable

public SpellInformation spellInformation;

and assign it in the Inspector?

First one worked…thanks for all your help - appreciated.

In case others are following this thread:

A refined search for the bones worked for me:

        boneComponents = info.Source.GetComponentsInChildren(typeof(Transform));
        numberOfBones = boneComponents.Length;
        for (int i = 0; i < numberOfBones; ++i)
        {
            string boneName = boneComponents[i].name;
            if (boneName.Equals("Bip01 L Hand") || boneName.Equals("L_Hand"))
            {
                hand = (Transform)boneComponents[i];
            }
        }