iTranslated by AI
How to Implement a Dialogue System in Unity: Easily Add Names and Lines
I have built a dialogue system that can be used for NPCs, enemies, and event lines.
It can be customized to start the dialogue upon a key press or contact.
This is implemented in Unity 6.

As you can see, names and dialogue lines can be added easily.
By leaving the name field blank, you can hide the name display.
While the UI is minimal, I believe it's easy to add your own.
Preparation
①
Add a Canvas to the Hierarchy.
Then, rename the Canvas to Talksystem_base.
base will be used when adding buttons or other elements later.
②
Add a Panel as a child of base (the Canvas).
Then, rename the Panel to Talksystem_windows.
windows serves as the background color for displaying dialogue and names.
Therefore, I recommend sizing the Panel to the bottom half of the Canvas and setting the color to gray.

③
Add two LegacyTexts as children of window (the Panel).
Rename one to Talksystem_dialog and the other to Talksystem_name.
I recommend a prominent color, such as a complementary color to the windows (Panel) color.
Changing only the name color is also an option.
Code
For this example, we will create it assuming NPC dialogue.
Add two MonoBehaviour scripts to your Assets.
Rename one to npc and the other to Talksystem.
Let's start by writing the NPC script.
You can simply copy and paste it.
using System.Collections.Generic;
using UnityEngine;
public class npc : MonoBehaviour
{
public bool Istouch; // For player collision detection
// Container for attaching the base
[SerializeField] GameObject Talksystem_base;
// Assign dialogue content to a list
[SerializeField] List<string> myTalk;
// Container for attaching Talksystem.cs
[SerializeField] Talksystem talkSystem;
// Container for inputting this character's name
[SerializeField] new string name;
void Start()
{
// Hide the dialogue UI at start
Talksystem_base.SetActive(false);
}
void Update()
{
// Prevent dialogue from starting twice
if (talkSystem.isTalking) return;
if (Istouch) // For player collision detection
{
if (Input.GetKeyDown(KeyCode.M)) // For player collision detection
{
// Show the dialogue UI
Talksystem_base.SetActive(true);
// Pass the name to talksystem.cs
talkSystem.SetName(name);
// Pass the dialogue content to talksystem.cs
talkSystem.StartDialogue(myTalk);
}
}
}
private void OnCollisionEnter2D(Collision2D collision) // For player collision detection
{
if (collision.gameObject.CompareTag("Player"))
{
Istouch = true; // Set the collision flag
}
}
private void OnCollisionExit2D(Collision2D collision) // For player collision detection
{
if (collision.gameObject.CompareTag("Player"))
{
Istouch = false;
}
}
}
Next, copy the following code into Talksystem.cs.
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class Talksystem : MonoBehaviour
{
public static Talksystem Instance;
[SerializeField] Text dialogueText;
[SerializeField] GameObject Talksystem_base;
[SerializeField] Text NameText;
private List<string> dialogue;
private int dialogNumber = 0;
public bool isTalking = false;
private void Awake()
{
Instance = this;
Talksystem_base.SetActive(false);
}
void Update()
{
if (!isTalking) return;
if (Input.GetKeyDown(KeyCode.Space))
{
dialogNumber++;
if (dialogNumber < dialogue.Count)
{
dialogueText.text = dialogue[dialogNumber];
}
else
{
EndDialogue();
}
}
}
public void StartDialogue(List<string> newDialogue)
{
dialogue = newDialogue;
dialogNumber = 0;
isTalking = true;
Talksystem_base.SetActive(true);
dialogueText.text = dialogue[dialogNumber];
}
public void EndDialogue()
{
isTalking = false;
Talksystem_base.SetActive(false);
}
public void SetName(string name)
{
NameText.text = name;
}
}
Attaching
Attach npc.cs to the object you want to have speak.
When you do, the following fields will appear:

- Attach the Canvas object you created at the beginning to
Talksystem_base. - Attach the
Talksystem.csfrom your Assets folder toTalk System. - Enter a name in
Nameif you want to display it. - Add dialogue lines using the "+" button in the
My Talkfield and enter them.
It might be easier if you implement a mechanism to find objects dynamically using Find in the code...
Final Preparation
In this code, the conversation starts when an object with the Player tag is touching and the M key is pressed.
Place an object with the Player tag (with Rigidbody2D and Collider2D) right next to the object with npc.cs attached.
Operation
When you press the M key while the player is touching, the name and dialogue appear.
Press the space key to move to the next line, and the UI disappears after reading to the end.
Pressing the M key again will restart the dialogue.
Afterword
Since this is my first article, it might have been hard to read...
If you have any questions, please let me know in the comments!
I will do my best to provide follow-up despite my limited vocabulary... Ω\ζ°)RIP
Thank you for reading until the end!
Discussion