Getting Started With Game Framework for Unity – Part 8 – Messaging Essentials

Skill level: Beginner / Intermediate
Tutorial time: 15 minutes
Implementing in your projects: 5 minutes

Part 1 – The Basics, Audio, Settings and Localisation
Part 2 – Level Setup and Selection
Part 3 – Game Loop, Score and Coins
Part 4 – Unlocking and In App Purchase
Part 5 – Applying a Theme
Part 6 – Worlds and Resources
Part 7 – GameItems, Lives, Health and Stars
Part 8 – Messaging Essentials
Part 9 – A Better Game

In this short tutorial we will look at how you can quickly use the messaging system to create a new reusable component that will show a message when the player loses a life. After this tutorial you should be able to easily do similar for other actions such as the score changing.


The messaging functionality in Game Framework allows for a much cleaner, simpler and more modular approach to your games making it easier for you to manage and maintain your games, and also simpler to build in new features.

To implement the example that we will create, you might traditionally have either:

  • Added extra code to the component that handles the player losing a life. This would include code to make updates to the score along with references to a UI gameobject that handles the message display.
  • Created a new component and checked for updates to the score in the Update loop.

Of the above however, the first example risks creating very complicated code and is not easily scalable if we want to add further conditions. The second example is better and more generic, however adds a lot of overhead due to the constant polling (especially if there are many such instances or components). It also requires close linking of different components making your game more complex and you code less reusable. It is also difficult to track the source of any errors that might occur.

Messaging is a push based approach where you register with the central messaging system that you are interested in messages of a certain type e.g. Enemy Killed. You will then be automatically notified when this happens without a further need for polling. You don’t need to worry about where to get the message from or who is sending it. If you have multiple enemy scripts, they can all send Enemy Killed messages and you will be notified. The component generating the messages doesn’t have to worry about what is being done when the message is received, or indeed if anyone is listening at all.

In Game Framework the above is all backed up by the Message Activity window that shows a log of all messages along with statistics on usage so you can easily track what is happening in your game


Building our Component

Before we build our component, go to your Game scene and under the existing Canvas gameobject, create a new gameobject called LifeLost with a text component similar to the below. It is this that will be enabled when a life is lost.


To build our component we will extend from the abstract generic component RunOnMessage.cs (FlipWebApps\GameFramework\Scripts\Messaging\Components\AbstractClasses). When you implement this component in your own class, you only need specify the type of message that you are interested in listening for and a method that should be called when a message is received. RunOnMessage takes care of registering with the message system. If you want to see closer how this is done then see the source code for that component. (Note, there are also other such base components including EnableDisableGameObjectMessaging and ShowValueAnimatedMessaging)

Create a new component in your game (Right Click in the Project Window | Create | New C# Script), rename it to OnLifeLostEnableGameobject and update it with the contents below:

using System.Collections;
using GameFramework.GameStructure.Players.Messages;
using GameFramework.Messaging.Components.AbstractClasses;
using UnityEngine;

public class OnLifeLostEnableGameobject : RunOnMessage<LivesChangedMessage>
    // The target gameobject that should be enabled.
    public GameObject Target;

    // The number of seconds after which to disable the gameobject again. 0 = never disable.
    public float DisableDelay;

    // Method that is run when a message is received.
    public override bool RunMethod(LivesChangedMessage message)
        // Enable the target gameobject
        // if we have specified a disable delay then start a coroutine to disable .
        if (DisableDelay > 0)
            StartCoroutine(DelayedDisable(Target, DisableDelay));
        return true;

    // Coroutine to disable the target gameobject after a given number of seconds.
    IEnumerator DelayedDisable(GameObject target, float delay)
        // Add transition script code here (see below)

        // wait for delay seconds.
        yield return new WaitForSeconds(delay);
        // disable the target (check for null incase the gameobject has been destroyed while we were waiting
        if (target != null)

Finally we need to connect some of the above together. Disable the LifeLost gameobject that you created earlier (we only want this to show when a life is lost). Add the newly created OnLifeLostEnableGameobject component to _SceneScope and set Target to the LifeLost gamobject, and Disable Delay to 2.

Run the scene and use the cheat functions to decrease the players lives. You should see that the life lost message is displayed each time.

Due to the flexibility of the messaging system we can now reuse this component anywhere in this or future games. It would also be simple to create similar components to do things like reset the player position, play some audio or perform some other event. We could easily extent upon other message types like HealthChangedMessage, PlayerCoinsChangedMessage etc…

Adding a transition effect

If you have the extras bundle, or the Beautiful Transitions asset you can add a nice animated effect. This can be done adding code similar to the below (depending on your desired effect) where the comment “Add transition script code here” is in our new component:

new Fade(target, 0, 1).Fade(1,0,delay - 0.5f).GetChainRoot().Start();

Instead of the above code you could also add the component for the effect you need to the Life Lost gameobject (e.g. Fade), set Auto Run to true, Auto Transition Out to true and Transition Out Delay to 1.7.

Wrap Up

That completes our look at messaging. For more information it is worth reading the documentation page on messaging that contains further information and a list of all the standard messages that are available. You should also look through the contents of the Messages editor window to see what is being sent. .

If you are using the free version please consider small price for the extras bundle for access to the tutorial files, themes and lots of other useful assets and samples. This also helps support our efforts to develop and maintain this framework.

If you like the framework then please leave a rating in the asset store.

If you have any questions or feature requests then let us know below.