x


BroadcastMessage between children?

If I have a hierarchy:

-Parent
--ChildOne
--ChildTwo

Can ChildTwo have a BroadcastMessage on it that ChildOne can "hear"?

more ▼

asked Jun 30 '11 at 06:35 PM

zachypin gravatar image

zachypin
71 35 38 38

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

The answer is yes, but you have to reference that child.

var target : GameObject;

function Start()
{
    target.BroadcastMessage("ClassToCall");
}
more ▼

answered Jun 30 '11 at 06:59 PM

zachypin gravatar image

zachypin
71 35 38 38

(comments are locked)
10|3000 characters needed characters left

Yes, it's called SendMessageUpwards() :)

EDIT: Or did you mean laterally (eg: a child on the same level of the hierarchy?) Because BroadcastMessage should do that by default.

more ▼

answered Jun 30 '11 at 08:36 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

(comments are locked)
10|3000 characters needed characters left

Depending on your other requirements, you could send the message from ChildOne's parent, or even its root (in case the hierarchy was many layers deep). e.g.

childOne.transform.parent.BroadcastMessage(...);

or

childOne.transform.root.BroadcastMessage(...);
more ▼

answered Jun 30 '11 at 07:00 PM

Molix gravatar image

Molix
4.8k 15 27 66

where does root look at?

Jun 30 '11 at 08:03 PM zachypin

The 'root' of a hierarchy is the object without a parent. In your example "Parent" is also the root. If "Parent" had its own parent, say, "Grandparent", then "Grandparent" would be the root.

See http://unity3d.com/support/documentation/ScriptReference/Transform-root.html

Jun 30 '11 at 08:15 PM Molix
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5054
x3316
x184
x83
x31

asked: Jun 30 '11 at 06:35 PM

Seen: 1301 times

Last Updated: Jun 30 '11 at 08:38 PM