Dialogue Trigger

Simple NPC dialogue with sequential messages, typewriter effect, and input to advance. No branching — just clean text sequences.

File
DialogueTrigger.cs
Unity
2022.3+
Level
beginner
Size
2.5 KB · 141 lines
Added
2026-03-11
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 11 serialized fields with their defaults.

Inspector
Script DialogueTrigger
Dialogue Content
Speaker Name NPC
Messages string · Size 0
UI References
Dialogue Panel None (GameObject)
Name Text None (TextMeshProUGUI)
Message Text None (TextMeshProUGUI)
Typewriter
Type Speed 0.03
Use Typewriter
Input
Advance Key Space
Events
On Dialogue Start ()
List is Empty
On Dialogue End ()
List is Empty
On Message Changed (int)
List is Empty

When to use it

Great for RPGs, adventure games, and visual novels that need simple NPC conversations or sign readouts. Use for linear dialogue sequences — tutorial popups, story beats, or item descriptions. For branching choices and conditions, upgrade to the full Dialogue System.

Setup

  1. Create a Canvas with a dialogue panel (panel, name text, message text)
  2. Attach DialogueTrigger to your NPC or sign object
  3. Fill in the speaker name and messages array
  4. Assign the UI references in inspector
  5. Call dialogueTrigger.StartDialogue() from your interaction system
  6. Press Space or Click to advance through messages
  7. For branching dialogue, use the full Dialogue System instead

Features

  • Sequential message display with configurable speaker name
  • Typewriter text effect with adjustable character speed
  • Click or keypress to skip typewriter or advance to next message
  • UnityEvents for dialogue start, end, and message change
  • Force-close method for interrupting dialogue externally
  • UI panel auto-show/hide on dialogue start and end

Inspector fields

Every field Dialogue Trigger exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Speaker NamespeakerNamestring"NPC"Dialogue Content
Messagesmessagesstring[]EmptyDialogue Content
Dialogue PaneldialoguePanelGameObjectNoneUI References
Name TextnameTextTextMeshProUGUINoneUI References
Message TextmessageTextTextMeshProUGUINoneUI References
Type SpeedtypeSpeedfloat0.03Typewriter
Use TypewriteruseTypewriterbooltrueTypewriter
Advance KeyadvanceKeyKeyCodeSpaceInput
On Dialogue StartOnDialogueStartUnityEventNo listenersEvents
On Dialogue EndOnDialogueEndUnityEventNo listenersEvents
On Message ChangedOnMessageChangedUnityEvent<int>No listenersEvents

Public API

What other scripts can call on DialogueTrigger.

MemberReturnsDescription
StartDialogue()voidStart the dialogue sequence.
ForceClose()voidForce-close the dialogue.
IsActive propertyboolIs a dialogue currently playing?

Requirements

  • Uses TextMeshPro (the TMPro namespace) for text.
  • Uses uGUI (UnityEngine.UI): needs a Canvas in the scene.
  • Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.

Common mistakes

You must assign the dialoguePanel, nameText, and messageText references in the Inspector — null references will silently skip display. The script uses TextMeshProUGUI, so make sure you import the TMP Essentials package. Call StartDialogue() from your interaction system; the script doesn't auto-trigger on proximity.

Source code

DialogueTrigger.cs, 141 lines, MIT licensed. Paste it into a new C# file of the same name.

DialogueTrigger.cs
C#
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using TMPro;
using System.Collections;

/// <summary>
/// Simple sequential dialogue system with typewriter effect.
/// Trigger from NPCs, signs, or any interactable object.
/// </summary>
public class DialogueTrigger : MonoBehaviour
{
    [Header("Dialogue Content")]
    [SerializeField] private string speakerName = "NPC";
    [SerializeField] [TextArea(2, 5)] private string[] messages;

    [Header("UI References")]
    [SerializeField] private GameObject dialoguePanel;
    [SerializeField] private TextMeshProUGUI nameText;
    [SerializeField] private TextMeshProUGUI messageText;

    [Header("Typewriter")]
    [SerializeField] private float typeSpeed = 0.03f;
    [SerializeField] private bool useTypewriter = true;

    [Header("Input")]
    [SerializeField] private KeyCode advanceKey = KeyCode.Space;

    [Header("Events")]
    public UnityEvent OnDialogueStart;
    public UnityEvent OnDialogueEnd;
    public UnityEvent<int> OnMessageChanged;

    private int currentMessageIndex;
    private bool isDialogueActive;
    private bool isTyping;
    private Coroutine typeCoroutine;

    /// <summary>Is a dialogue currently playing?</summary>
    public bool IsActive => isDialogueActive;

    /// <summary>
    /// Start the dialogue sequence.
    /// </summary>
    public void StartDialogue()
    {
        if (isDialogueActive || messages == null || messages.Length == 0) return;

        isDialogueActive = true;
        currentMessageIndex = 0;

        if (dialoguePanel != null)
            dialoguePanel.SetActive(true);

        if (nameText != null)
            nameText.text = speakerName;

        OnDialogueStart?.Invoke();
        ShowMessage(currentMessageIndex);
    }

    private void Update()
    {
        if (!isDialogueActive) return;

        if (Input.GetKeyDown(advanceKey) || Input.GetMouseButtonDown(0))
        {
            if (isTyping)
            {
                // Skip typewriter, show full message
                if (typeCoroutine != null)
                    StopCoroutine(typeCoroutine);
                messageText.text = messages[currentMessageIndex];
                isTyping = false;
            }
            else
            {
                // Advance to next message
                currentMessageIndex++;
                if (currentMessageIndex < messages.Length)
                {
                    ShowMessage(currentMessageIndex);
                }
                else
                {
                    EndDialogue();
                }
            }
        }
    }

    private void ShowMessage(int index)
    {
        OnMessageChanged?.Invoke(index);

        if (useTypewriter)
        {
            if (typeCoroutine != null)
                StopCoroutine(typeCoroutine);
            typeCoroutine = StartCoroutine(TypeMessage(messages[index]));
        }
        else
        {
            messageText.text = messages[index];
        }
    }

    private IEnumerator TypeMessage(string message)
    {
        isTyping = true;
        messageText.text = "";

        foreach (char c in message)
        {
            messageText.text += c;
            yield return new WaitForSecondsRealtime(typeSpeed);
        }

        isTyping = false;
    }

    private void EndDialogue()
    {
        isDialogueActive = false;

        if (dialoguePanel != null)
            dialoguePanel.SetActive(false);

        OnDialogueEnd?.Invoke();
    }

    /// <summary>
    /// Force-close the dialogue.
    /// </summary>
    public void ForceClose()
    {
        if (typeCoroutine != null)
            StopCoroutine(typeCoroutine);
        EndDialogue();
    }
}