Search Unity

My queue won't cycle through every sentence

Discussion in 'Scripting' started by Ezzie24, Nov 4, 2020.

  1. Ezzie24

    Ezzie24

    Joined:
    Nov 2, 2020
    Posts:
    2
    Hai everyone!

    I recently started with Unity and I have a problem I can't quite seem to find the answer to on Google. I followed Brackeys tutorial on a dialogue system in Unity (
    ) till just before he animates the text. Thanks to Unity updates my script is a little bit different than his, but except for two lines I had to add to make my dialogue box working it should all be the same. I also commented those lines out so it's back at the last working version (where I get the text in my Console via Debug instead of in the dialogue box). I also added some Debug.Log lines to count the amount of sentences in my array/queue.

    So these are my files. First off, Dialogue Manager, which I think where the problem is as the other files are exactly like his project in the description.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class DialogueManager : MonoBehaviour
    6. {
    7.    public Text nameText;
    8.    public Text dialogueText;
    9.    private Queue<string> sentences;
    10.    // Start is called before the first frame update
    11.    void Start()
    12.    {
    13.        sentences = new Queue<string>();
    14.    }
    15.    public void StartDialogue (Dialogue dialogue)
    16.    {
    17.        //nameText=GameObject.Find("Name").GetComponent<Text>();
    18.        //nameText.text = dialogue.name;
    19.        Debug.Log ("Starting conversation with " + dialogue.name);
    20.        sentences.Clear();
    21.        foreach (string sentence in dialogue.sentences)
    22.        {
    23.            sentences.Enqueue(sentence);
    24.            Debug.Log(sentences.Count);
    25.        }
    26.        DisplayNextSentence();
    27.        Debug.Log(sentences.Count);
    28.    }
    29.    public void DisplayNextSentence ()
    30.    {
    31.        Debug.Log(sentences.Count);
    32.        if (sentences.Count == 0)
    33.        {
    34.            EndDialogue();
    35.            return;
    36.        }
    37.        string sentence = sentences.Dequeue ();
    38.        //dialogueText=GameObject.Find("Dialogue").GetComponent<Text>();
    39.        //dialogueText.text = sentence;
    40.        Debug.Log(sentence);
    41.    }
    42.    void EndDialogue()
    43.    {
    44.        Debug.Log("End of conversation.");
    45.    }
    46. }
    47.  
    48.  
    Then we have the Dialogue Trigger
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class DialogueTrigger : MonoBehaviour
    5. {
    6.    public Dialogue dialogue;
    7.    public void TriggerDialogue ()
    8.    {
    9.        FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
    10.    }
    11. }
    12.  
    13.  
    Lastly we have the Dialogue
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [System.Serializable]
    5. public class Dialogue
    6. {
    7.    public string name;
    8.    [TextArea(3, 10)]
    9.    public string[] sentences;
    10. }
    11.  
    12.  
    I added a counter in the Dialogue Manager and it says the queue has two sentences in it, which is what I set it to be. When I click the Start Conversation Button (triggers DialogueTrigger.TriggerDialogue, so starts the dialogue) I get a message with whom I started a conversation, then I get the two sentences.Count messages, then I get "text 1", which is what I put the first sentence as. It then says there is one sentence left. When I click the continue button I trigger DialogueManager.DisplayNextSentence, but it says there aren't any sentences left and I get the end of dialogue message.

    When I up the amount of sentences in the Array/Queue to say three, the first count indeed counts three. The second one counts 2 and after pressing continue it counts 0 and ends the conversation.

    Am I doing something stupid here that resets my amount of sentences in the Queue to zero when it activates DisplayNextSentence for the second time? That seems quite impossible with my code. Is it a bug in Unity or C#?
    I'm really new to this, so sorry if the answer is somewhere on Google. I tried my best finding it. I also hope I gave enough info...

    Help is really appreciated.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you calling sentences.Dequeue or sentences.Clear anywhere else in your code?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Is there any chance you have more than one DialogueManager in your scene?
     
    Bunny83 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    Right.
    DialogueManager.DisplayNextSentence
    is not a static method so you have to actually call the method on the actual instance. The most common mistake is to setup a reference to a prefab and not the actual instance in the scene. So you would call DisplayNextSentence on the prefab which of course doesn't have any strings inside it's queue.
     
  5. Ezzie24

    Ezzie24

    Joined:
    Nov 2, 2020
    Posts:
    2
    Thanks for the replies!

    No, this is all the code I have


    I only added the dialogueManager script to my dialogueManager object and to the name text object. If I remove it from the latter, it gives a problem with line 25 Which is sentences.Clear...


    I tried my hardest to understand this... the sentences are set within the start conversation button, because that's what triggers the conversation. Do you mean that's the problem? Should I use the start conversation button to say the first sentence and then change a variable so the continue button can get the rest inside of it?
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    I have actually issues to understand or visualize that ^^. Do us all a favour and place some "meaningful" debug logs in your code. By meaningful I mean logs which can be uniquely matched / tracked down to the line of code it came from. Just logging a single integer value in several places is just a mess. Next you should pass the gameobject as second "context" parameter to your Debug.Log calls. This will automatically highlight this context object when you click on a debug log message in the console. This generally helps to track down which objects you're working with.

    Since your issue is mainly your "DisplayNextSentence" method I would recommend to do this in the first line of the method:

    Debug.Log("DisplayNextSentence queued sentences: " + sentences.Count, gameObject);


    Run your game, press your buttons and check the log messages. So when you click on them in the console, the Unity editor will show you which object you actually worked on. My guess was that you have setup your button onClick event to not call the method on your actual gameobject instance in the scene but you may have called it on the prefab in your project instead.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    So you do have multiple DialogueManagers? That's going to cause a problem with your FindObjectOfType call, as you can't control which instance gets returned.
     
    Joe-Censored likes this.