Dialogue system for multiple dialogues in the scene

Hello, basically my player will walk through the scene and in some specific points he will collide with a collider that triggers a dialogue, it’s actually a text box that display some hints to pass to the next level (I know it’s not exactly a dialogue but I hope you could understand). So I used this tutorial by Brackets for dialogue system. The problem is that if I had to put multiples points in the scene it won’t work, it will only work for the first point I collide, or it will display another text from the other point I have yet not collided. I haven’t found another tutorial on Youtube for multiple dialogues in the scene. Any help will be appreciated.

Tutorial Link

ColliderDialogue.cs attached to Canvas and a point collider:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColliderDialogue : MonoBehaviour
{
    public GameObject DialogueBox;
    DialogueManager dialogueManager;
    DialogueTrigger dialogueTrigger;
    public bool PlayingDialogue = false;

    private void Awake()
    {
        dialogueManager = FindObjectOfType<DialogueManager>();
        dialogueTrigger = FindObjectOfType<DialogueTrigger>();
    }

    // Update is called once per frame
    private void Start()
    {
        DialogueBox.SetActive(false);
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            dialogueManager.DisplayNextSentence();
        }
        if(dialogueManager.playingDialogue == true)
        {
            PlayingDialogue = true;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            DialogueBox.SetActive(true);
            dialogueTrigger.TriggerDialogue();
            Destroy(gameObject);
        }
    }

DialogueManager.cs attached to a empty game object ‘DialogueManager’:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
    public GameObject DialogueBox;
    public TextMeshProUGUI nameText;
    public TextMeshProUGUI dialogueText;
    private Queue<string> sentences;

    public bool playingDialogue = false;

    public bool endDialogueBox = false;
    void Start()
    {
        sentences = new Queue<string>();
    }

    public void StartDialogue (Dialogue dialogue)
    {
        playingDialogue=true;

        nameText.text = dialogue.name;

        sentences.Clear();

        foreach (string sentence in dialogue.sentences)
        {
            sentences.Enqueue(sentence);
        }

        Time.timeScale = 0;

        DisplayNextSentence();
    }

    public void DisplayNextSentence()
    {
        if(sentences.Count == 0)
        {
            EndDialogue();
            return;
        }

        string sentence = sentences.Dequeue();
        StopAllCoroutines();
        StartCoroutine(TypeSentence(sentence));
    }

    IEnumerator TypeSentence(string sentence)
    {
        dialogueText.text = "";
        foreach (char letter in sentence.ToCharArray())
        {
            dialogueText.text += letter;
            yield return null;
        }

    }

    public void EndDialogue()
    {
        Debug.Log("End of conversation");
        DialogueBox.SetActive(false);
        Time.timeScale = 1;
        endDialogueBox = true;
    }

}

Dialogue.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Dialogue 
{
    public string name;
    [TextArea(3, 10)]
    public string[] sentences;
}

DialogueTrigger.cs attached to point collider:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour
{
    public Dialogue dialogue;

    public void TriggerDialogue()
    {
        FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
        
    }
}

This is because you are using the Destroy(gameObject) statement in your OnTriggerEnter2D method, which destroys the collider object after the first collision. Consequently, the script is unable to trigger dialogues for other points.

To handle multiple dialogue points without destroying them, you can modify your code to use a boolean flag or another mechanism to ensure that the dialogue is triggered only once for each point. Here’s a modified version of your code:

ColliderDialogue.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColliderDialogue : MonoBehaviour
{
    public GameObject DialogueBox;
    DialogueManager dialogueManager;
    DialogueTrigger dialogueTrigger;
    public bool PlayingDialogue = false;

    // Flag to track whether dialogue has been triggered for this point
    private bool dialogueTriggered = false;

    private void Awake()
    {
        dialogueManager = FindObjectOfType<DialogueManager>();
        dialogueTrigger = FindObjectOfType<DialogueTrigger>();
    }

    private void Start()
    {
        DialogueBox.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            dialogueManager.DisplayNextSentence();
        }
        if (dialogueManager.playingDialogue == true)
        {
            PlayingDialogue = true;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player" && !dialogueTriggered)
        {
            DialogueBox.SetActive(true);
            dialogueTrigger.TriggerDialogue();
            dialogueTriggered = true; // Set the flag to prevent repeated triggering
        }
    }
}

Remember that you need to remove the Destroy(gameObject) line from the OnTriggerEnter2D method in your ColliderDialogue.cs script to keep the collider objects alive.

This solved the problem of the others points not working, but still it’s not showing the correct text attached to each point. Example: if I make 2 points in the scene, when I trigger the first it will display the text from the other point.