0
\$\begingroup\$

The first script the manager is attached to new empty gameobject: I want the conversation to start automatic when running the game so i'm calling the method TriggerDialogue in the manager:

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

public class DialogueManager : MonoBehaviour
{
    public DialogueTrigger dialoguetrigger;

    private Queue<string> sentences;

    // Use this for initialization
    void Start ()
    {
        sentences = new Queue<string>();

        dialoguetrigger.TriggerDialogue();
        DisplayNextSentence();
    }

    public void StartDialogue(Dialogue dialogue)
    {
        Debug.Log("Starting conversation with " + 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();
        Debug.Log(sentence);
    }

    void EndDialogue()
    {
        Debug.Log("End of conversation.");
    }
}

Next the DialogueTrigger:

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

public class DialogueTrigger : MonoBehaviour
{
    public Dialogue dialogue;

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

Last the Dialogue:

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

[System.Serializable]
public class Dialogue
{
    public string name;

    [TextArea(3, 10)]
    public string[] sentences;
}

In the editor I filled the all 3 TextAreas each one with a sentence.

Hello world

Hi everyone

I have woke up

When running the game it's showing only to the Hi everyone it's not showing the I have woke up. And it also not showing the "End of conversation." in the log.

\$\endgroup\$
1
  • \$\begingroup\$ Is it safe to assume you're using a button or another script to call DisplayNextSentence() a third time? We're not able to see that part in the code shown here. \$\endgroup\$
    – DMGregory
    Jul 13, 2018 at 13:40

2 Answers 2

0
\$\begingroup\$

I don't know if that will even count as an answer, but clearly it's better to close this question by flagging it. However, debugging questions are allowed?

Solution: pay attention to when you call DisplayNextSentence. You never call it the third time.

\$\endgroup\$
4
  • \$\begingroup\$ Yes, debugging questions are allowed, if they provide a Minimal Complete Verifiable Example. Here we have most of one - the only question is whether DisplayNextSentence is being called by a button or another piece of code we don't see here. \$\endgroup\$
    – DMGregory
    Jul 13, 2018 at 13:41
  • \$\begingroup\$ @DMGregory The answer is no. DisplayNextSentence is not being called by any other button/s it's only called once in the DialogueManager. What I wanted to do is when running the game that the conversation will start automatic and will continue automatic without any button need to continue. The idea is to make some kind of monologue when the game is running first time. Later it will become a dialogue with options to the user to chose answers. \$\endgroup\$ Jul 13, 2018 at 13:47
  • \$\begingroup\$ Sounds like you want a looping coroutine with a delay between lines. \$\endgroup\$
    – DMGregory
    Jul 13, 2018 at 13:49
  • \$\begingroup\$ The first thing why it didn't call all the sentences, I should call the method DisplayNextSentence(); in the DialogueManager in the Update. And using a flag helper to stop the conversation once it called Enddialogue. And now I will use a coroutine to make a delay between each sentence to make the same action but with a delay too. \$\endgroup\$ Jul 13, 2018 at 14:06
0
\$\begingroup\$

My solution. and it's working the way I wanted.

I'm using the variable numberofsenteces to help me getting the number of sentences in the class Dialogue. Then I loop over the sentences + 1 since I want to get to the "End of conversation."

In the start I'm calling StartCoroutine(DisplayNextSentenceWithDelay()); Inside DisplayNextSentenceWithDelay I'm looping over the sentences with delay of 2 seconds between each sentence.

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

public class DialogueManager : MonoBehaviour
{
    public DialogueTrigger dialogueTrigger;

    private Queue<string> sentences;
    private int numberofsentences;

    // Use this for initialization
    void Start ()
    {
        sentences = new Queue<string>();
        dialogueTrigger.TriggerDialogue();
        StartCoroutine(DisplayNextSentenceWithDelay());
    }

    public void StartDialogue(Dialogue dialogue)
    {
        Debug.Log("Starting conversation with " + dialogue.name);

        sentences.Clear();
        numberofsentences = dialogue.sentences.Length;

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

        DisplayNextSentence();
    }

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

        string sentence = sentences.Dequeue();
        Debug.Log(sentence);
    }

    public IEnumerator DisplayNextSentenceWithDelay()
    {
        for (int i = 0; i < numberofsentences + 1; i++)
        {
            yield return new WaitForSeconds(2);
            DisplayNextSentence();
        }
    }

    void EndDialogue()
    {
        Debug.Log("End of conversation.");
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Do you even need numberofsentences? while (sentences.Count > 0) then out of the loop call EndDialogue. \$\endgroup\$ Jul 13, 2018 at 15:43
  • \$\begingroup\$ @CandidMoon I tried to make a loop over the sentences inside DisplayNextSentenceWithDelay() but there are only 2 sentences not 3. Only if I'm getting the number of sentences from the Dialogue it's the correct number of sentences. sentences.Count in the DialogueManager show 2 and sentences.Length in the Dialogue class show 3. That's why I'm using the numberofsentences. But I guess I did something wrong. Why the sentences in the dialogueManager show only 2 sentences ? \$\endgroup\$ Jul 13, 2018 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .