Sending a message from a parent to a child works smoothly enough, but how does one go the other way? So that if something happens to a child object it affects the parent in a certain way?
Is that what that does? I looked at that but “Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour” confused me. Does this work with parents then? I am playing with it but haven’t had success yet…
yep ancestor = parent. here’s an example from the machine gun script in the FPS tut (sorry don’t have time now to elaborate more ; ):
if (Physics.Raycast (transform.position, direction, hit, range))
{
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles)
{
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
Thanks. That’s very useful to know!
The reason it says ancestor instead of parent is because it sends the message not just to the parent, but any parent of that parent, and so on (if they exist). In other words the message goes all the way back to the root. Although personally I rarely have anything deeper than just parent/child.
–Eric