Revamping the UI: Consider Events Handled

The final major step was to redo the in game level UI because it was filled with my programmer art.  At the same time, I found a way to make the entire code base for the system cleaner and more efficient.  The major component of these changes involved me learning about Events and delegates, something that I had been having trouble grasping the usefulness of until then. The first step was to add the art into the scene.  The scene's full transformation is shown below.

Before and after in game UI

This new layout included a few new things not in our original game design.  We added the ability to speed up or slow down the game, we limited the number of robots provided for each level and tracked this under the spawn robot button. and finally we added points to the game.

With the addition of all these changes it forced me to take another look at how the UI was being updated.  I originally had one script that stored all of these label and updated as other objects updated that main script.  This wouldn't work for the points because then each robot would need to reference this script for every instance that caused it to earn points.

This is where the EventHandler came in.  After some additional research I found one post that really helped me understand why and how I should be using events and delegates.  I would create an event for when a robot scored points, when a robot made it to the end, when a placeable was placed, and when a robot spawned.  These events are all stored in my EventHandler as shown below.

public class EventHandler : UnitySingleton<EventHandler> {

    public delegate void RobotGainedPoints(int points);
    public static event RobotGainedPoints OnRobotPoints;
    public static void CallRobotPoints(int points){
        if(OnRobotPoints != null){

    public delegate void RobotCreated();
    public static event RobotCreated OnRobotCreated;
    public static void CallRobotCreated(){
        if(OnRobotCreated != null){

    public delegate void RobotDestroyed(GameObject robot);
    public static event RobotDestroyed OnRobotDestroyed;
    public static void CallRobotDestroyed(GameObject robot){
        if(OnRobotDestroyed != null){

    public delegate void RobotScored(GameObject robot, int points);
    public static event RobotScored OnRobotScored;
    public static void CallRobotScored(GameObject robot, int points){
        if(OnRobotScored != null){
            OnRobotScored(robot, points);

The EventHandler stores the event, the delegate, and the function that can be called to trigger the event.  All of these are static so I can access them from anywhere.  This allowed me to easily have each label subscribe to their appropriate events and update whenever that event was call.  Thus, simplifying my code and cleaning up the MainCameraGUI script substantially.

This event system allowed me to track stats for our level completion screen that now appears after each level is complete.  Now that I learned how and where to implement events I feel my programming abilities have gotten that much better.