Dialogues in games are interesting mechanics for interacting with the environment
Steps
- Create script Dialogue.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class Dialogue { [SerializeField] private string name; [SerializeField] private List< string > _dialogues = new (); public string Name => name; public List< string > Dialogues => _dialogues; } |
- Create script DialogueManager.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; public class DialogueManager : MonoBehaviour { [SerializeField] private TMP_Text _title; [SerializeField] private TMP_Text _dialogue; [SerializeField] private List<Dialogue> _dialogues = new (); private int _nameIndex; private int _dialogIndex; private void Start() { StartDialog(); } private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { NextDialog(); } } public void StartDialog() { DisplayDialog(); } private void DisplayDialog() { _title.text = _dialogues[_nameIndex].Name; _dialogue.text = _dialogues[_nameIndex].Dialogues[_dialogIndex]; } private void NextDialog() { if (_dialogIndex != _dialogues[_nameIndex].Dialogues.Count - 1) { _dialogIndex++; } else { if (_nameIndex == _dialogues.Count - 1) { Debug.Log( "Dialog end" ); return ; } _nameIndex++; _dialogIndex = 0; } DisplayDialog(); } } |
- Create two Text Mesh Pro. First for name, second for dialogues

- Create empty object and attach script DialogueMananger.cs

Press the space bar to go to the next dialog
Result
