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!

Card, video, graphics.

RTX 2080 Coming Soon – What will you do?

With the RTX line of graphics cards coming soon, everybody is flipping out. Can you really afford a brand new graphics card? I can’t! So WTF am I posting for? I’m posting to see who else is hoping the RTX line will drop prices of last line graphics cards. I see the 10 series being relevant for quite a while longer, I’m going to be keeping an eye on Amazon 1080 prices for price drops. After all, the 1080 is still going to dominate the market for a long time.

Card, video, graphics.

Are you patient enough to wait for prices to drop?

As a gamer, and most likely a millennial, we all want the shiniest and newest GPU. Most of us can’t afford it, though! Most of us don’t have patience either! What are we to do? I, for one, do have the patience. I’m going to wait for price drops, watching every place I can to hopefully get a new one. I’m not looking for used, because – mining. There’s too many used GPUs out there that have been beat up for months, then resold. You’ll not be happy if you get a new-used GPU, and it craps out on you in 3 weeks.

What can you do to satisfy the urge?

I just got a new SSD to satisfy my urge. A couple hundo got me the Samsung 970 Pro 512 GB, and it did not disappoint. What if you want to bring the price down, while you wait? How about Intel Optane? Heard of it? You can boost your existing hard drive’s performance with wicked caching voodoo magic, and Intel Optane Memory. I haven’t tried Optane Memory yet, personally, but it’s been in my cart about a hundred times now.

Upgrade SSD Drive

Are your pockets overflowing with cash, and you’re buying the new GPU?

Can I get an application for wherever you work? Just kidding, but seriously. What are your plans with the new GPU? Are you going to be number one on a benchmark site, do some CAD/3D modeling, or just poop on everybody in Roblox? I’m excited for the new cards to drop, and I can’t wait to hear about everybody’s experience with them!

Surface Book 2 15 Inch – In a mobile design environment

Current Setups

I take a TLDR approach to my reviews here, I don’t want to bore you with 15 pages of junk, that you’ll never read. If you don’t want to read all the juicy stuff, skip to the bottom part, labeled “The TLDR Part”. So here’s the scoop!

I’ve been using the Microsoft Surface Book 2 15″ lately, as a machine that I work on projects when I’m not in the office or at home. I have a design machine at work with a Quadro P5000 and an i7 7700k, and one at home with SLI GeForce1070s and an AMD FX 8350. These machines are great for what I do in Unity 3D, and many other applications. I can say the 1070s are slower in light baking, but I paid personal money for that – so it’s going to stay that way until the RTX Quadros release, and prices drop for other GPUs.

Enough about me, what about the Surface Book 2 15″?

Specs:

Display – 15” PixelSense™ Display with up to 3240 x 2160 resolution, 3:2 aspect ratio, 10 point multi-touch, and ink

Storage – 256GB, 512GB, or 1TB PCIe SSD

CPU – 8th Gen Intel® Core™ i7-8650U quad-core processor, 4.2GHz Max Turbo

GPU – NVIDIA® GeForce® GTX 1060 discrete GPU w/6GB GDDR5 graphics memory

Usability:

  • General Feel
    • Keyboard is about as good as it gets, for a laptop – amazing feel to it.
    • Touchpad is great, you get more gestures than you’d expect, and the size of the touchpad is about perfect.
    • Weight is a little much, but if you look at teardowns, it has batteries to justify the weight.
    • Size is magical, very slim machine, doesn’t feel huge like a 17″ mobile workstation.
    • Noise, when you’re under heavy work loads with the CPU, noise isn’t too bad, GPU is another story – sounds like a jet engine taking off
    • Battery life, is amazing! Just as advertised, and I’m impressed.
    • Speed, surprisingly good! I did notice going from a desktop 7700k, or 8350, to the 8650 laptop i7, light baking is a little slower, but not bad.
  • Peripherals
    • Mouse, I’d recommend a Surface Precision Mouse, the other options are not awesome for a design(or gaming) environment.
    • Dial, Have you heard of the Surface Dial? It’s pretty cool, but I’d wait until the gen 2 dial comes out, it will have a 3D joystick feel to it.
    • Pen, I didn’t think I’d have a need for a pen on a computer, what for? But when I use tablet mode, I use the Surface Pen for sure, I love it.
    • Dock, this is less of a mobile experience thing, but the Surface Dock is nice for connecting 2 screens super fast.
    • THE BEST PART! They all match. This is super satisfying for the OCD, thank the gods of Microsoft for matchyness!

The TLDR Part:

So the conclusion? I have used the Surface Book 2 15″ for a few months now, and I can say that the experience is much different than I expected. Normally you try to look past the marketing stuff, because they’re just trying to make it look good, but it is good. Mind blown by the 1060 in the Surface Book 2 15″. I imagined that anything less than a 1070 would have been garbage for the torment that I put my computers through, but man was I wrong! Between the Gen 8 i7, and the 1060, you have enough power to do pretty much anything you’d like, without lag ruining your day.

If you’re looking for an extremely portable, workstation worthy, consumer grade, super laptop, get the Surface Book 2 15″. You will not be disappointed!

Creating apps is easy

Unity – Simplified Application Development

How can you make application development more simple?

This is not a game. Have you considered finding a more simple approach to developing your apps and programs? Unity 3d(Free for Personal) compiles applications for a very wide variety of platforms including Windows, Linux, iOS, Android, and the web. As a primarily Java programmer, with a PHP backend, I’ve done a lot of other projects with other languages.

Let’s simplify the logic.

My favorite so far has been visual scripting with PlayMaker for Unity. Visual scripting is something that I used to think wasn’t anything special. Why? Well it’s not actually “scripting”, right?

Scripting simplified with PlayMaker

Wrong. Visual scripting contains all of the same logic that a normal programming/scripting language contains. You need to set your variables, interact with objects, set hooks and interrupts to trigger actions, etc. Everything is the same, but it’s more readable, and easier on the eyes. You can visually understand how the code is flowing, without having to read through all the code. Look at the above picture, how much code would this take? It’s a simple script that fades the camera in, waits, then fades it out. Well, you’d have to write a function that fades the camera, with some arguments that handle the fade in/fade out and sets a variable for original position right? 30 lines of code? 50? How about 3 code blocks instead? Debug that if something’s not working right, 2 clicks, not 2 hours of sifting through your code. AMAZING what PlayMaker can do to speed up coding, and simplify troubleshooting.

Playmaker

Let’s simplify the design

Obviously, as a 3d development application, Unity does 3d well. What about 2d? Just a regular app, not a game? Right now I use a deadly combination of WordPress for Unity and Next-Gen UI to make the best looking applications out there. These two plugins provide a seamless, and simple, approach to designing interfaces for applications. It’s a drag and drop, zero code, approach to your users’ interactions. I use these two along side PlayMaker to develop some of the most complicated applications I’ve worked on. I consider myself lucky to be alive, in a time where we have so many tools like this coming out to simplify our every day work. You need to understand, these tools are not just creating user interfaces and simplifying them, they’re giving you a simple way to animate them, put them to action, compile them for multiple platforms, and monetize them.

WordPress for Unity
NGUI: Next-Gen UI

Endless opportunities

The Unity Asset Store has endless amounts of free, or paid, assets for your projects that make it a whole lot easier to make money! No matter who you are, you can develop something extraordinary, with zero knowledge up front. The design/development of applications is coming together, and becoming easier, making it one simple platform – no longer two separate skill sets. One single person can now develop and design an amazing creation without having to do a whole lot of learning. It’s time for you to dive in.

Building 3D Models the Easy Way

3D Designing Made Simple – for 3D printing, Minecraft, and more!

I have been searching for a long time, for a 3D design software that isn’t confusing. I don’t have time to learn how to use complicated programs, and I don’t have the money to spend on expensive programs. The plan is to come up with a simple solution, that is fast, and cheap or free. Seriously? Am I out of my mind to think that there’s a program out there that’s cheap or free, that does 3D design, and is easy? I found out that I wasn’t out of my mind, there’s a program, and I’m here to tell you what it is and how easy it is to use. It’s 100% free! It’s called TinkerCad.

My Project

This part is the project that I’m working on, that made me want to do this. Skip this section, if you’re just trying to get a dang answer out of me. I recently purchased 3 Nvidia Tesla M2090’s. They are advanced processing units(APU). They are generally used in a server environment, for high performance computing. What they do is perform tasks, that normally take a long time, in a short period of time. Your average every day processor has 2-8 cores, the Nvidia Tesla M2090 has 512 cores. This is an older unit, so the number 512 may seem high, but there are newer ones out there that blew my mind.

So what’s the problem with my super duper processor thingy? It’s passively cooled, which means there’s no fans. Normally a server has like 50 fans in it(ok, maybe 10), so these units don’t need fans built into them. Well I’m not putting them in servers, I’m putting them in desktops. There’s no where near enough airflow in a desktop to cool these things(I figured that out quickly). In my personal machine, I put a lot of time into making sure the inside of my desktop is like a wind tunnel, seriously. It’s pretty amazing how much airflow I have running through that thing. My desktop is nowhere near good enough though. So I designed an enclosure with a spot for a blower fan from an old laptop.

I laid in bed with my Surface Book 2, and designed this in tablet mode with the Surface Pen, that’s how easy this is! I was trying to find a simple program that would let me add shapes, and subtract one shape from another, and this was it! It’s super dang simple. The website is tinkercad.com. Let me demonstrate. I’ll take this cube, and subtract a cylinder from it, to make a hole in the center.

First thing we do, is select the cube from the toolbar on the right side of the screen.

Box tool tinkercad

Then, all you do is click somewhere on the screen to place it!

tinker cad editing box

How are we going to remove a shape from it?

Let’s use the cylinder here, just to put a simple hole, right through the middle of it.

tinkercad cylinder tool

Just like before, click the cylinder tool, then click inside the box to place it.

Cylinder placed in box

This doesn’t quite look like a hole, does it? That’s because it isn’t! What we need to do, is edit the cylinder, and turn it into a hole. So click the cylinder, and you should get a menu that pops up in the top right that looks like this.

tinkercad object menu

In this menu, there’s a solid or hole toggle. You want to click hole, and this is what it’ll look like.

cylinder hole in box

Still doesn’t quite look like a hole, does it? What we need to do, is group these 2 objects. It is this way, because maybe you want a hole in one object, but not in another. Well, if 2 objects intersect, you only have to select 1 of them to get the hole placed in 1 object instead of having a hole in both intersecting objects.

Click and drag, as if you’re highlighting multiple icons on your desktop, to select both objects. Then in the top right, above the menu we used before, find the group button. This will make the hole in the cube visible.

tinker cad group button

After we click that, this should be the result that you got.

grouped box and hole cylinder

Simple, right?!?!

What else can you do?

There’s a lot you can do with this 3d design tool, and it’s amazing! TinkerCad is a pretty dang sweet tool, and it’s super easy to use. I seriously can’t believe it’s free. Are there any tips or tricks you can share with me? Let me know in the comments.

Building 3D Models the Easy Way