Hi
Do anybody knows how to send messages or parameters to anywhere within a Unity project?
-“SendMessage” is sending within a game object
-“SendMessageUpwards” is sending within a game object and to a parent of it
-“BroadcastMessage” is sending within a game object and down to its children
But how can one send messages (or parameters) to a script anywhere within the whole project.
Thanks for a littel hint
visualizer.ch
i dont know bout the entire project, but as far as within a single scene, any objects… you can iterate through them.
for(var go : GameObject in GameObject.FindObjectsOfType(GameObject)){
go.SendMessage("blah", SendMessageOptions.DontRequireReciever);
}
Depending on your needs, you may want to roll your own event or messaging solution, ranging from dead simple to very complex. The wiki has a few examples.
You could use any of the pre-existing message functions with GameObject.Find I think. Something like:
GameObject.Find("/Parent/Child").BroadcastMessage(...etc.
But as Jasper said, if this isn’t enough the wiki has a couple of really easy to use systems that do this for you.
If you are looking to send a message to a single script, such as a GameController - that should only appear once in the scene - your best bet is to use a singleton pattern.
static GameController _instance;
public static GameController Instance {
get {
if (_instance == null){
GameObject go = GameObject.FindGameObjectWithTag("GameController");
_instance = go.GetComponent<GameController>();
}
return _instance;
}
}
public void TestFunction(){}
Then you can talk to GameController (or whatever your class is) anywhere in the project by saying things like:
GameController.Instance.TestFunction()