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”
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?
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.
Dann schreibt doch eine Übersetznug dahinter… (ok, dumme idee)
Translation: Write a translation behind it… (ok, stupid idea)
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 .
@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
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)
{
}