Confused about sending message to child object.

I’m trying to send a message to a game object parented to the object of this script but it’s not working out.

GameObject weapon;

void Start(){

		weapon = transform.Find("MP40");

}

public void MessageSend(){

		weapon.SendMessage("Blah", 1.0f, SendMessageOptions.DontRequireReceiver);
}

Error message is line 5

Cannot implicitly convert type UnityEngine.Transform' to UnityEngine.GameObject’

Isn’t transform.Find supposed to find the child object named here? What am I doing wrong?

Transform.Find return type is Transform, so you need to access the attached GameObject:

weapon.gameObject.SendMessage("Blah", 1.0f, SendMessageOptions.DontRequireReceiver);

Hey there,

If you look at your code you can see transform.find. The function returns a Transform.

You are then trying to take the return Transform and set weapon to it. Weapon is a GameObject A Transform is not the same thing as a GameObject. A GameObject has a transform.

I think I am saying Transform and GameObject too much :slight_smile:

To use that code you have to go weapon = transform.Find(“MP40”).gameObject OR gameObject.find(“MP40”).

Regards,