Desperate for Dialogue System Help

I know this is probably super obvious to everyone else, but I’ve been stuck for days trying to create dialogue in Unity2D. I have a dialogue box that runs and works on startup. However no matter what I do I can’t get that same dialogue box to appear when I interact with an NPC. I have tried adding an on click event and button to it to no avail. I currently have the dialogue trigger and a button connected to the NPC. Any help is really appreciated.

public class DialogueManager : MonoBehaviour
{
    public Text nameText;
    public Text dialogueText;

    public Animator animator;
    private Queue<string> sentences; //Queues are first in, first out for data
    // Start is called before the first frame update
    void Start()
    {
        sentences = new Queue<string>();
    }

    public void StartDialogue (Dialogue dialogue)
    {
        animator.SetBool("isOpen", true);
        nameText.text = dialogue.name;
        sentences.Clear(); //gets rid of previous sentences so new ones can be loaded

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

    public void DisplayNextSentence ()
    {
        if (sentences.Count == 0) //checks to see if there are anymore sentences
        {
            EndDialogue();
            return;
        }
        string sentence = sentences.Dequeue();
        StopAllCoroutines();
        StartCoroutine(TypeSentence(sentence));
    }

    IEnumerator TypeSentence (string sentence) //this function uses an array to make one letter appear at a time
    { //may not be working, add larger delay between letters and check
        dialogueText.text = "";
        foreach (char letter in sentence.ToCharArray())
        {
            dialogueText.text += letter;
            yield return null;
        }
    }
    void EndDialogue()
    {
        Debug.Log("End of Convo");
        animator.SetBool("isOpen", false);
    }
}
//to make a caharacter speak, add the dialougue trigger script to that game object
public class DialogueTrigger : MonoBehaviour
{
    public Dialogue dialogue;

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

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "npc") //attempting to make npc speak on collision
        {
            FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
            Debug.Log("Convo w/ Frog");
        }
    }
}

I see only two lines of Debug.Log()… you gotta step up that game! GO NUTS!

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

I will work through these solutions when I get home. I had debug through most of the script earlier and took it away because it was working. Will try to sprinkle it back in, but I’m not sure that’s the issue. I do have a debug for starting the npc dialogue that’s not appearing.
Currently not getting any errors in Unity or Visual Studio so that doesn’t seem to be the issue. Sorry if this seems dismissive I’m just at my wits end with what I feel like should be simple.

Follow the chain, verifying or fixing each part one at a time. Do not even THINK about another thing until you fix each thing one at a time:

  • are you hitting ANYTHING with a collider?
  • are you hitting the CORRECT thing with a collider?
  • if not, print the names of the collider… what are you hitting that is wrong?
  • are you finding a dialog manager? Why not? Pause, find it by hand in the scene. Is it there? Why not?
  • are you calling StartDialog on it?
  • are you giving it the correct dialog to start?
  • etc.

It’s all just steps, one step at a time. Each one of those things can trivially be learned via tutorials, then you can take your individual piece knowledge back and make sure you have your project set up properly.

Remember: it’s NEVER just code. Code works together with the scene and assets and EVERYTHING has to be set up perfectly. Use Debug.Log() to figure out where your discrepancies are.