Skip to main content

Raising Game Event

After creating and configuring events, the final step is triggering them in your game logic. This page shows how Game Events work and how to raise them in your scripts.

Complete the Visual Workflow
  1. βœ… Create events β†’ Game Event Creator
  2. βœ… Configure actions β†’ Game Event Behavior
  3. βœ… Raise events ← You are here

🎯 How Game Events Work​

Game Events decouple event raising from action execution:

Traditional Approach:

// ❌ Tightly coupled - door logic knows about sound, animation, etc.
public class Door : MonoBehaviour
{
public AudioSource audioSource;
public Animator animator;
public UIManager uiManager;

public void Open()
{
audioSource.Play();
animator.SetTrigger("Open");
uiManager.ShowNotification("Door opened");
// Logic scattered across multiple dependencies
}
}

Game Event Approach:

// βœ… Decoupled - door only knows "something happened"
public class Door : MonoBehaviour
{
[GameEventDropdown]
public GameEvent onDoorOpened;

public void Open()
{
onDoorOpened.Raise(); // Actions configured in Inspector
}
}

Key Difference: Actions (sound, animation, UI) are configured visually in Event Behavior, not hardcoded in scripts.


πŸ“ Basic Usage: Raising Events​

Step 1: Reference the Event in Your Script​

using TinyGiants.GameEventSystem.Runtime;
using UnityEngine;

public class DoorController : MonoBehaviour
{
[GameEventDropdown] // Smart Inspector picker
public GameEvent onDoorOpened;

[GameEventDropdown]
public GameEvent onDoorClosed;

public void OpenDoor()
{
// Your door logic here
onDoorOpened.Raise(); // Trigger the event
}

public void CloseDoor()
{
// Your door logic here
onDoorClosed.Raise();
}
}

Step 2: Assign Event in Inspector​

The [GameEventDropdown] attribute provides a type-safe searchable dropdown:

GameEvent Dropdown

Features:

  • πŸ” Fuzzy Search: Type to filter events by name
  • πŸ“ Categorized: Events grouped by database and category
  • πŸ”’ Type Safety: Only shows compatible event types
  • ⚑ Quick Access: No manual asset dragging needed

Alternative: Without [GameEventDropdown]​

You can also use a standard public field:

public GameEvent onDoorOpened;  // Standard ScriptableObject field

Inspector View:

Standard Object Field

Workflow:

  1. Locate event asset in Project window (Event Database)
  2. Drag & drop into Inspector field

Recommendation: Use [GameEventDropdown] for better workflowβ€”it's faster and type-safe.


🎨 Typed Events (With Arguments)​

Events can carry data to actions.

Void Events (No Data)​

[GameEventDropdown]
public GameEvent onGameStart;

void Start()
{
onGameStart.Raise(); // No arguments
}

Single Argument Events​

[GameEventDropdown]
public GameEvent<float> onHealthChanged;

private float health = 100f;

public void TakeDamage(float damage)
{
health -= damage;
onHealthChanged.Raise(health); // Pass current health value
}

Type Safety: Dropdown only shows GameEvent<float> events, preventing type mismatches.


Sender + Argument Events​

[GameEventDropdown]
public GameEvent<GameObject, DamageInfo> onPlayerDamaged;

public void ApplyDamage(DamageInfo damageInfo)
{
// Sender = this GameObject, Args = damage info
onPlayerDamaged.Raise(this.gameObject, damageInfo);
}

Use Case: Actions need to know who triggered the event and what data to process.


πŸ”’ Type Safety in Action​

The dropdown automatically filters events based on field type:

public class ScoreManager : MonoBehaviour
{
[GameEventDropdown]
public GameEvent<int> onScoreChanged; // Only shows GameEvent<int>

[GameEventDropdown]
public GameEvent<int> onLevelUp; // Only shows GameEvent<int>

private int score = 0;

public void AddScore(int points)
{
score += points;
onScoreChanged.Raise(score); // Pass integer score
}
}

Dropdown Filtering:

Available Events for GameEvent<int>:
βœ… OnScoreChanged (int)
βœ… OnLevelUp (int)
βœ… OnComboMultiplier (int)
❌ OnPlayerDeath (void) β€” Filtered out (wrong type)
❌ OnDamage (float) β€” Filtered out (wrong type)

Why This Matters: Catches type errors at edit time, not runtime.


πŸ”„ Canceling Scheduled Events​

If your event uses delay or repeat settings (configured in Game Event Behavior), you can cancel execution:

[GameEventDropdown]
public GameEvent repeatingSoundEvent;

void StartAmbientSound()
{
repeatingSoundEvent.Raise(); // Starts repeating (based on Behavior config)
}

void StopAmbientSound()
{
repeatingSoundEvent.Cancel(); // Stops scheduled execution
}

Use Cases:

  • Player leaves trigger zone β†’ Cancel ambient sounds
  • Game paused β†’ Cancel timed events
  • Object destroyed β†’ Cleanup scheduled actions

πŸ”§ Advanced: Inspector Listener Control​

Rarely needed, but you can disable Inspector-configured actions at runtime:

[GameEventDropdown]
public GameEvent myEvent;

void DisableCutsceneUI()
{
myEvent.SetInspectorListenersActive(false);
// Inspector actions won't fire, only code listeners
}

void EnableCutsceneUI()
{
myEvent.SetInspectorListenersActive(true);
// Inspector actions fire again
}

Use Cases:

  • Temporarily disable UI updates during cutscenes
  • Switch between action sets based on game state

πŸ’‘ Complete Workflow Example​

Let's build a complete door system using the visual workflow.

Step 1: Create Events​

In Game Event Creator:

Event Editor Create

  • Create OnDoorOpened (void event)
  • Create OnDoorClosed (void event)

Step 2: Configure Actions​

In Game Event Behavior:

Event Behavior Configure

OnDoorOpened Event:

  • Action: AudioSource.PlayOneShot(doorOpenSound)
  • Action: Animator.SetTrigger("Open")
  • Action: ParticleSystem.Play() (dust effect)

OnDoorClosed Event:

  • Action: AudioSource.PlayOneShot(doorCloseSound)
  • Action: Animator.SetTrigger("Close")

Step 3: Write the Script​

using TinyGiants.GameEventSystem.Runtime;
using UnityEngine;

public class DoorController : MonoBehaviour
{
[GameEventDropdown]
public GameEvent onDoorOpened;

[GameEventDropdown]
public GameEvent onDoorClosed;

private bool isOpen = false;

public void ToggleDoor()
{
if (isOpen)
{
isOpen = false;
onDoorClosed.Raise(); // All actions fire automatically
}
else
{
isOpen = true;
onDoorOpened.Raise(); // All actions fire automatically
}
}

// This method can be called from:
// - Button OnClick in Inspector
// - Collision/Trigger detection
// - Other game systems
}

Step 4: Assign Events in Inspector​

Door Inspector Setup

  1. Select DoorController GameObject
  2. Use dropdown to assign OnDoorOpened event
  3. Use dropdown to assign OnDoorClosed event

Done! No sound, animation, or VFX references in scriptβ€”all configured visually.


πŸ†š Why Better Than UnityEvents?​

Traditional UnityEvent approach has limitations that Game Events solve:

Traditional UnityEvent Limitations​

// ❌ Problem 1: Configuration scattered across many GameObjects
public class Button1 : MonoBehaviour
{
public UnityEvent onClick; // Configured in Button1's Inspector
}

public class Button2 : MonoBehaviour
{
public UnityEvent onClick; // Configured in Button2's Inspector
}

// ❌ Problem 2: Hard to find all usages
// Need to manually search every GameObject in scene

// ❌ Problem 3: No central control
// Can't globally enable/disable button sounds

// ❌ Problem 4: Duplication
// Same sound/VFX setup repeated in 50 buttons

Game Event Advantages​

// βœ… Solution: All buttons raise the same event
public class ButtonController : MonoBehaviour
{
[GameEventDropdown]
public GameEvent onButtonClick; // Same event for all buttons

public void OnClick()
{
onButtonClick.Raise();
}
}

Benefits:

FeatureUnityEventGame Event
Centralized Config❌ Per GameObjectβœ… One Event Behavior
Find All Usage❌ Manual searchβœ… Event Finder
Global Control❌ Change 50 objectsβœ… Change one event
Reusability❌ Copy-pasteβœ… Reference same asset
Conditional Logic❌ Code requiredβœ… Visual condition tree
Debugging❌ Inspector onlyβœ… Flow Graph visualization

When to Use Each​

Use UnityEvents:

  • Simple one-off callbacks (e.g., tutorial button)
  • Component-specific logic (e.g., slider updates its own label)
  • No need for reusability

Use Game Events:

  • Reusable logic (e.g., all button clicks play same sound)
  • Complex sequences (e.g., cutscenes, door puzzles)
  • Need central control (e.g., mute all UI sounds)
  • Want visual debugging (Flow Graph)

❓ Troubleshooting​

Cause: No GameEventManager in scene.

Solution:

Open Game Event System via the Unity toolbar:

Tools > TinyGiants > Game Event System

Click the "Initialize Event System" button, creating a Game Event Manager GameObject (Singleton) in your scene.


Cause: No databases assigned in GameEventManager.

Solution:

  1. Select GameEventManager in scene
  2. Inspector β†’ Databases section
  3. Add your event databases

Cause: No events match the field type.

Example:

[GameEventDropdown]
public GameEvent<string> textEvent; // Needs GameEvent<string>

// But your databases only have:
// - GameEvent (void)
// - GameEvent<int>
// - GameEvent<float>

Result: No matching events!

Solution: Create events of the correct type using Game Event Creator.


Event Doesn't Fire​

Checklist:

  1. βœ… Is event asset assigned in Inspector?
  2. βœ… Is Raise() being called? (add Debug.Log to verify)
  3. βœ… Are actions configured in Game Event Behavior?
  4. βœ… Are conditions passing? (check condition tree)
  5. βœ… Is GameEventManager in scene?
Visual Workflow Complete!

You've now learned the complete visual workflow:

  1. βœ… Create events in Event Creator
  2. βœ… Configure actions in Event Behavior
  3. βœ… Raise events with UnityEvents or GameEventDropdown

Result: Decoupled, maintainable, designer-friendly game logic!

From Visual to Code

This page covers visual workflow (raising events in scripts with Inspector assignment). For advanced code techniques (runtime listeners, conditional triggers, event chains), see Runtime API.