Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How to make custom events?

Discussion in 'Unity Analytics' started by Sushin, Jul 28, 2015.

  1. Sushin


    May 2, 2015
    I'm having a lot of trouble just creating custom events to send for analytics.
    Mostly, all I want to do is sent events for when the player reaches certain areas along with a time stamp.

    I tried pasting this sample (and modifying it) but all I get is an error message in the console.
    Code (csharp):
    1.   using System.Collections.Generic;
    3.   int totalPotions = 5;
    4.   int totalCoins = 100;
    5.   string weaponID = "Weapon_102";
    6.   Analytics.CustomEvent("gameOver", new Dictionary<string, object>
    7.   {
    8.     { "potions", totalPotions },
    9.     { "coins", totalCoins },
    10.     { "activeWeapon", weaponID }
    11.   });

    I don't really know what I'm doing, but I have basic integration with my Unity project. Are there any examples I could take a look at?
  2. marc_tanenbaum


    Unity Technologies

    Oct 22, 2014
    So the code sample relies on you understanding a tiny bit of C#. But fear not, we're here to help!

    All C# code lives in classes. Each class represents some functionality of your game. For example, things you probably know — BoxCollider, Renderer and Transform — are classes that enable collision detection, rendering and position/rotation respectively. Now you don't need to write those classes since Unity's done it for you. But custom events — since they're custom — have to be written by you. Here's one way the code snippet might be written as a C# class.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.Analytics;
    5. public class GameOverTracker : MonoBehaviour {
    7.     public bool someConditionHasBeenMet = false;
    9.     public void Update() {
    10.         if (someConditionHasBeenMet) {    //See below
    11.             SendEventOnGameOver ();
    12.         }
    13.     }
    15.     void SendEventOnGameOver() {
    16.         int totalPotions = 5;
    17.         int totalCoins = 100;
    18.         string weaponID = "Weapon_102";
    19.         Analytics.CustomEvent ("gameOver", new Dictionary<string, object> {
    20.             { "potions", totalPotions },
    21.             { "coins", totalCoins },
    22.             { "activeWeapon", weaponID }
    23.         });
    24.         someConditionHasBeenMet = false;
    25.     }
    26. }
    So this class is called GameOverTracker. It extends MonoBehaviour, which means that it can be attached to a GameObject. This class has two methods: Update and SendEventOnGameOver. Methods are things that a class can do. In any MonoBehaviour, the Update method gets called (executed) once per frame of your game. SendEventOnGameOver will only be called if someConditionHasBeenMet is equal to true. As long as someConditionHasBeenMet equals false, SendEventOnGameOver will never be called.

    Now, what would it take for someConditionHasBeenMet to be true? I can't answer that. Only you know that, because it's different for every game. Maybe it's crossing a finish line, or killing all the bad guys, or solving a puzzle, or dying. Whenever the game is over, set someConditionHasBeenMet to true, and that will send your custom event.

    Here's a pretty nice blog post on writing your own MonoBehaviours, to help you with these fundamentals:

    Hope that helps!
  3. Sushin


    May 2, 2015
    That helped a lot. I got it figured out.
    I was mostly having a lot of issues with syntax but this cleared everything up. :]
    marc_tanenbaum likes this.