- Home /
Communicating between objects without any physical connection?
I'm trying to find a way to send a message from one object to another without them having any physical interaction. There are no collisions, no projectiles, and they are not parented in any way?
What would be the best way to do this?
What I'm trying to achieve is a system whereby I can tell if something is within my camera's Field of View. And then whether that object is or isn't effect it's behavior accordingly.
Any help much appreciated. Thanks!
Answer by PrimeDerektive · Jan 28, 2011 at 08:35 PM
var allObjects = Object.FindObjectsOfType(GameObject);
for (var go : GameObject in allObjects){ if (go.renderer.isVisible){ // if this gameobject is visible by any camera go.GetComponent(MyScript).DoStuff(); } }
Note that you could of course use FindObjectsWithTag or FindObjectsOfType(MyScript) or any other method of specifying what type of objects you actually want this functionality to be restricted to. Finding ALL the objects in the scene might get a little intense depending on what you have going on.
Answer by zharramadar · Jan 28, 2011 at 07:33 PM
Find the object with GameObject.Find() and then use GameObject.SendMessage() to call a method in this object you want. This will call the specified method in any script attached to the GameObject you found. For example:
GameObject obj = GameObject.Find("player"); obj.SendMessage("Hit");
This will find the object named player and execute any or all methods called Hit in any script attached to the player gameobject.
If you want to simply manipulate the gameobject you found, just use the methods in the variable obj (in the case of the example above)
I do not support suggesting Send$$anonymous$$assage for what you described. Your example is in C#, so $$anonymous$$ono's event handling would be appropriate.
Indeed, Send$$anonymous$$assage might make the object too relaxed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101552im_/https://answers.unity.com/themes/thub/images/avi.jpg)