x


Calling a function of another object's child?

I got an individual script attached to two different weapon-objects: weapon_1 and weapon_2. Both scripts have a different version of the same function called Fire(). Both are children of the parent-object: weapons.

Here's the problem: The fire-button is placed outside this hierarchy, but I want to be able to reach whatever weapon's script is active at a given time (either weapon_1 or weapon_2).

Since the name of the script varies depending on which weapon is active, I can't use the method of searching for the script's name:

var myWeapons : weapons = FindObjectOfType(weapons);
myWeapons.Fire();

Another method I tried was using the "BroadcastMessage", but since that only works for the object itself or its children, and in my case I want to reach it from outside the hierarchy, I tried this approach:

var weapons : GameObject;
...
weapons = GameObject.Find("weapons");
weapons.BroadcastMessage("Fire");

Unfortunately this won't work. It seems when using the "BroadcastMessage" on another object than the one making the call, the function skips checking the children of the targeted object.

So, is there another way of targeting the child of another object dynamically like in the second proposed but invalid method above?

more ▼

asked Jul 14 '10 at 12:30 AM

Daniel 1 gravatar image

Daniel 1
31 8 8 11

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

1 answer: sort oldest

It appears the error was due to my own stupidity of having renamed the "weapons"-object without editing the second line in the second sample. Having done so the "BroadcastMessage" does indeed look for functions in the targeted object's children.

more ▼

answered Jul 14 '10 at 01:01 AM

Daniel 1 gravatar image

Daniel 1
31 8 8 11

(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:

x5268
x437
x427
x31
x8

asked: Jul 14 '10 at 12:30 AM

Seen: 1618 times

Last Updated: Jul 14 '10 at 12:30 AM