Unity 3D – Sending Messages Between Objects

It’s very common to need to use a Messaging/Event system in Unity 3D. Honestly, it’s much easier to use an asset to do things like this, but not everybody wants to buy an asset to do everything they do in Unity 3d. That’s why you’re a developer right? To do it yourself! In case you’d like a built out system, check out Event Manager or Event Man. While those can be helpful, it’s also helpful to understand how the event system works behind the scenes, so please do read on.

I learn all my skills on PluralSight, if you’ve never checked it out you should! I work hard to get people hooked up with good deals for my readers, so take advantage of the Unlimited Access Free Trial I set up for you guys! After some PluralSight videos, the ladies won’t be able to keep their hands off you! Sorry to the ladies, if you’re not into ladies and they can’t keep their hands off you, you probably shouldn’t have clicked the link…

What is the Messaging System?

To put it in my own words, and make it simple to understand in one sentence – An event system is a pair of scripts that allow you to run a function on the destination object, by calling a trigger from the source object.

Is that easy enough? We’ll see. Once you grasp the concept of the messaging system, you’ll think of a million and a half reasons to use it. No more using GameObject.Find. I’m sick of it! Using GameObject.Find is not a reliable way to do business between objects, and doing multiple objects at once is cumbersome. With the messaging system, you can have multiple game objects subscribing to the same triggers and they will both fire off functions at the same time. Like frickin magic!

Let’s get the show on the road!

We’re going to get started by creating a new project of course. In unity hub, hit new.

Now give that baby a name, we’re going to call ours “Messaging Test”.

Note: You can see I’m using a 2019.3 alpha version. This messaging replaces GameObject.SendMessage, there were issues with that method. You can still use GameObject.SendMessage, but that’s not what we’re doing here.

So, what I’m going to do here is use a couple game objects to rotate the camera, to make it easier to position the objects around the camera, let’s select the camera from the hierarchicy.

and move it to position 0,0,0 from the inspector.

That will allow us to position objects 1 meter in front and 1 meter behind the camera, so we can rotate 180 degrees to see each object.

Now we’ll create our two objects. You can pick whatever you’d like to do this with, but I’m going to do a cube and a sphere. Then you can see the change in objects when the camera rotates. Do this by right clicking in the hierarchy and selecting 3D Object->Cube and then do it again selecting 3D Object-> Sphere.

Now you should be able to see the cube and sphere in the inspector, as well as the scene view. This is what it should look like in the inspector.

From here, we’re going to want to position the cube and sphere on each side of the camera. Select the cube from the hierarchy and set it’s position to 0,0,5. Now select the Sphere from the hierarchy and set it’s position to 0,0,-5. If you find the objects in the scene view, it should look like the next picture. The cube and sphere should be on opposite sides of the camera object. You can also flip over to the game view and see the cube.

Let’s start scripting now!

The first thing we’re going to set up is the listener. This will receive messages sent by other game objects, and it’s going to be placed on the camera. So select the camera in the hierarchy and click add component in the inspector. In the add component search type script, and click add new script

We have to give it a name, let’s call it “MessageListener” and click Create and Add.

Now in the inspector you should see a Message Listener(Script) component. Right click that component and click Edit Script. That’ll open the script in whatever editor you have as default. I’m using Visual Studio.

The listener script

We’re going to be using the UnityEngine.EventSystems namespace and creating a public interface that implements IEventSystemHandler. The interface lists the two methods we can call from the trigger. Then we’ll implement that interface in our class, and define the methods. Both methods will rotate the camera 180 degrees. Here’s the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public interface MessageListenerInterface : IEventSystemHandler
{
    // functions that can be called via the messaging system
    void Message1();
    void Message2();
}

public class MessageListener : MonoBehaviour, MessageListenerInterface
{
    public void Message1()
    {
        this.transform.Rotate(0,180,0);
    }

    public void Message2()
    {
        this.transform.Rotate(0, 180, 0);
    }
}

You can write that out, or copy and paste the code into the editor, now save that and switch back over to Unity3D.

Prepping the GameObjects

We need to prep the GameObjects to accept a click. To do this we’ll add a collider to the cube and sphere.

Select the Cube in the hierarchy, click add component in the inspector, and type collider. You’ll get a list of all the colliders available, we’re going to use a box collider for the cube, so select that.

Now we’ll do the same for the sphere. Select the Sphere in the hierarchy, click add component in the inspector, and type collider. We’re going to use a sphere collider for the sphere, so select that.

Let’s go back to the cube and add the trigger script. Select the cube in the hierarchy and do add component, search script, select new script, we’ll name this one Message1Trigger, and click create and add. Now right click the Message1Trigger(script) component and click edit script.

First Trigger Script

Here we’re going to be using the UnityEngine.EventSystems namespace again, but we won’t be implementing anything special. We’ll create a gameobject variable and executing the Message1 method on it. After we create the script we’ll define that variable in the inspector. Here’s the script for Message1Trigger:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Message1Trigger : MonoBehaviour
{
    public GameObject target;
    void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0)){
            ExecuteEvents.Execute<MessageListener>(target, null, (x, y) => x.Message1());
        }
    }
}

Save that and switch back over to Unity3D. Now we’ll define the GameObject called “target”. Make sure the cube is still selected in the hierarchy, and you should see a new option under the Message 1 Trigger (Script) component called “Target”. Drag the Main Camera from the hierarchy onto the box next to Target. You should see the value switched from None to Main Camera.

Next we’re going to do the same for the sphere. Select the sphere in the hierarchy and do add component, search script, select new script, we’ll name this one Message2Trigger, and click create and add. Now right click the Message2Trigger(script) component and click edit script.

Second Trigger Script

Here we’re going to be using the UnityEngine.EventSystems namespace again, but we won’t be implementing anything special. We’ll create a gameobject variable and executing the Message2 method on it. After we create the script we’ll define that variable in the inspector. Here’s the script for Message2Trigger:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Message2Trigger : MonoBehaviour
{
    public GameObject target;
    void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ExecuteEvents.Execute<MessageListener>(target, null, (x, y) => x.Message2());
        }
    }
}

Save that and switch back over to Unity3D. Now we’ll define the GameObject called “target”. Make sure the sphere is still selected in the hierarchy, and you should see a new option under the Message 2 Trigger (Script) component called “Target”. Drag the Main Camera from the hierarchy onto the box next to Target. You should see the value switched from None to Main Camera.

That’s it! You’ve set up a messaging system. Let’s test that thing out! Click play on the top of the scene view, and it should switch over to game view automatically.

Your game view should show the cube when you first start

Give that bad boy a click, and it’ll rotate the camera to put the sphere in view.

Boom like magic the camera flips 180 degrees programatically, and the crowd goes wild! Click the sphere, and watch it happen again!

Back to the cube we go, you did a full 360 McTacoTwist. That’s 360 more degrees than you’ve ever rotated in your life. Thank you Unity3D for making all this possible!

What do you guys think? Are you going to be implementing more messaging into your Unity3D projects? I know I am! Let me know if you have any questions, or want to see anything else. Thanks for reading!

Cell Phone Signal

Signal Boosters – Single VS Multi Carrier

Cell phone signal boosters are not an uncommon accessory for personal or business use. I have one in my car, and it’s an amazing difference driving around with a device that gives you better service everywhere you go. The setup for these is simple, and they just work!

What does single and multi carrier mean?

Multi Carrier
Multi carrier signal boosters are the most common type. You plug them in and they boost the signal for (almost) any carrier. In the United States, I haven’t heard of a carrier that doesn’t get boosted by them – maybe very small carriers. The only issue with these is that there’s a FCC regulation limiting the strength of a multi carrier cellular signal booster.

A good example(and the one I use) of a multi carrier booster is the WeBoost Drive 4G-X.

I’ve been blown away by the functionality of this, and it’s pretty awesome having it right in my car. There are also home solutions like the WeBoost Connect.

Single Carrier
Single carrier signal boosters are the ultimate if you’re in an area where there is no way you’re getting any service at all. We recently deployed a single carrier cell signal booster to a job site and it worked wonders! They are using a CradlePoint IBR900for internet, and it’s an amazing product that provides a high quality cellular internet connection. Until recently, we haven’t found a place that these don’t work. There’s a completely dead spot for miles in Wisconsin where our job site is. So we installed a single carrier signal booster called the Cel-Fi Go X.

The results are pretty amazing, we went from zero connection to a 15Mbps connection with zero help on my end. I just shipped it out and they installed it, and they are not techy people. So if they can do it, you can too. The obvious drawback of a single carrier cell signal booster is the price. This is a case where you do get what you pay for though, at more than twice the price, we got a product that actually worked for our scenario and we were blown away by the results.

Telecommunication network above city, wireless mobile internet technology for smart grid or 5G LTE data connection, concept about IoT, global business, fintech, blockchain

What are your use cases?

Whether you’re at home using a hotspot for your computer, or using your cell phone, I think signal boosters are a huge win that do not disappoint.

Leave a comment, and let me know what you are using signal boosters for. There are many use cases like mobility in vehicles, internet of things, mobile offices, and home use. Any ideas?