Help me with multiple dialogues.

Hi, I'm a beginner developer on Unity.

I have a problem with the development of my 2D game - dialogues. In fact, I made one dialogue, but it is attached to another NPC. I don't know how to fix it, I searched on YouTube and more, but it didn't help. Please help me with this. Thanks in advance. Here are the codes:

Dialogue code:

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

[System.Serializable]

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

Dialogue Trigger code:

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

public class DialogueTrigger : MonoBehaviour
{
    public Dialogue dialogue;

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

Dialogue Manager code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class DialogueManager : MonoBehaviour
{
    public TextMeshProUGUI text;
    public TextMeshProUGUI name;

    public Animator boxAnim;
    public Animator startAnim;

    private Queue<string> sentences;

    private void Start()
    {
        sentences = new Queue<string>();
    }

    public void StartDialogue(Dialogue dialogue)
    {
        boxAnim.SetBool("boxOpen", true);
        startAnim.SetBool("startOpen", false);

        name.text = dialogue.name;
        sentences.Clear();

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

    public void DisplayNextSentence()
    {
        if(sentences.Count == 0)
        {
            EndDialogue();
            return;
        }
        string sentence = sentences.Dequeue();
        StopAllCoroutines();
        StartCoroutine(TypeSentence(sentence));
    }

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

    public void EndDialogue()
    {
        boxAnim.SetBool("boxOpen", false);
    }
}

Dialogue Animator code:

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

public class DialogueAnimator : MonoBehaviour
{
    public Animator startAnim;
    public DialogueManager dm;

    public void OnTriggerEnter2D(Collider2D other)
    {
        startAnim.SetBool("startOpen", true);
    }

    public void OnTriggerExit2D(Collider2D other)
    {
        startAnim.SetBool("startOpen", false);
        dm.EndDialogue();
    }
}

[quote=“stormy_ua”, post:1, topic: 949504]
attached to another NPC
[/quote]

The “attachment” of this stuff to any particular character is almost certainly not going to be anywhere in code.

It will be a function of who has this code connected to them and what data is plugged into it.

In Unity the code is only a tiny part of the problem. Be sure you understand how the scene and prefabs and any other assets are set up and connected.

Dialogs are pretty hairy things all around, certainly not what I would consider a beginner topic.

Dialog system fundamentals and structure:

https://discussions.unity.com/t/807140/2

There are also some free packages you could start from:

Fungus: https://fungusgames.com

Inkle: https://www.inklestudios.com

Even if you don’t end up using either of those, you could review them for structure because it is almost certain that they have already solved all the same problems you are trying to solve.