Sending Messages between GameObjects

hi ereryone,

i am just curious:
when i want to call a public function of script attached on another gameobject i do:

GameScript gameref = (GameScript) GameObject.Find("theGameObjectIwantToCall").GetComponent("GameScript");
gameref.doSomething();

i also know i can send messages via GameObject.SendMessage to any gameObejcts attached to the caller.

BUT, how can i send a message / call a function on every GameObject of a certain type which are not attached to the caller but like siblings attached to the same scene?
e.g. sending a message to all robots: “self destruct” :wink:

do i have to use something like GameObject.SendMessageUpwards??
or do i have to manually by find all objects by using GameObject.FindGameObjectsWithTag(“theTag”) and process the resulting list of gameobjects by myself?

hope someone can help…

daniel

Hey, someone else from Hamburg! :slight_smile:

FindGameObjectsWithTag sounds like the candidate you search for. I usually tend to put those kind of objects into a specific array or tag them and then call the appropriate function or do a SendMessage. But take care as SendMessage is about 100x slower than a direct function call.

thank you martin.

things are a bit clearer now. i was just wondering if unity had some kind of messaging system as e.g. actionscript3 has…

happy to see other hamburg-unity-users :smile:
(i suppose think you are one of the few german speakers who could actually understand my nickname)

Uh, no clue wih your nick. Just hanging around you mean?

**cough yes!
but more like “lying around”. not going out like my peers but to stay at home an hack some more code…

anyway i have another question though:

do you know the difference between
GameObject.SendMessage
and
Component.BroadcastMessage??

prost :smile:

Ehm, no, no clue what the difference is.

Where you’re located in HH? I’m from downtown, Grindel.

Wieso redet ihr beiden nicht einfach deutsch?
(Translation: Why do you don’t talk in german?) :slight_smile:

Because I would find that disrespectful to the other forum users… :slight_smile:

Dann schreibt doch eine Übersetznug dahinter… (ok, dumme idee)
Translation: Write a translation behind it… (ok, stupid idea) :slight_smile:

Der Uterschied ist, glaube ich, dass man mit Irgendwas.BroadcastMessage auf von allem auf alles zugreifen kann; d.h.: es gibt transform.BroadcastMessage , component.BroadCastMessage , usw…
SendMesage wird größtenteils dazu verwendet, um auf untergeordnete Objekte zuzugreifen (parents/children).

(Translation: I think the difference is that you can access from everything to everything with something.BroadcastMessage; there are transform.BroadcastMessage , component.BroadCastMessage , etc…
SendMessage is mostly used to access childs or parents of an object.)

@martin: i’m located in st. pauli - yay!!
i love drunk people on the streets :smile:.

@gaston: u know abroad germans don’t like to be identified as germans. so they pretend not to be german…

hmmm think its time asking the docs: (Component.BroadcastMessage)
“Calls the method named methodName on every MonoBehaviour in this game object or any of its children.”
and in Component.SendMessage it says:
“Calls the method named methodName on every MonoBehaviour in this game object.”

as i would understand both are for sending Messages within a gameobject (i mean a complex one with child gameobjects) but BroadcastMessage also sends messages to all children while SendMessage is just sending messages to any MonoBehavior attached to the current gameobject.

i still feel a bit confused whether there is a way of using these functions to speak to neighbour / sibling gameobjects :sweat_smile:

There’s also Object.FindObjectsOfType() you could use to look for all your robot-scripts and call a specific function.

var robots :RobotScript = FindObjectsOfType(RobotScript);
for (var rob :RobotScript in robtos) {
    rob.Selfdestruct();
}

thank you adrian.
this looks very elegant…

Hallo everyone, I find the tags in Unity work well for situations like this sometimes, it’s quite fast too - Find() and FindObjectsOfType() is quite slow, tags are cached internally I think.

var robots = GameObject.FindGameObjectsWithTag("Robot");
foreach (var item in robots)
{

}

Cheers from Japan : )