Search Unity

Resolved dialogue system skipping text

Discussion in 'Scripting' started by bernardwalrus44, Feb 25, 2024.

  1. bernardwalrus44

    bernardwalrus44

    Joined:
    Feb 25, 2024
    Posts:
    2
    hey, I'm a new game dev and I've been following this brackeys tutorial


    everything works fine except for some reason the button i have to start the dialogue skips straight to the final sentence and I'm not sure how to make it go in order, from one sentence to the next. this is the code below
    would be great if anybody knows how to fix this.
    Code (CSharp):
    1. public class dialogueManager : MonoBehaviour
    2. {
    3.    
    4.     public TextMeshProUGUI nametext;
    5.     public TextMeshProUGUI dialoguetext;
    6.  
    7.     private Queue<string> sentances;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         sentances = new Queue <string>();
    13.        
    14.     }
    15.  
    16.     public void startdialogue (dialogue Dialogue)
    17.     {
    18.         nametext.text = Dialogue.name;
    19.      
    20.         sentances.Clear();
    21.  
    22.         foreach (string sentance in Dialogue.sentances)
    23.         {
    24.  
    25.             sentances.Enqueue(sentance);
    26.  
    27.  
    28.  
    29.             desplaynextSentance();
    30.  
    31.         }
    32.     }
    33.  
    34.  
    35.  
    36.     public void desplaynextSentance()
    37.     {
    38.        
    39.  
    40.  
    41.         if (sentances.Count == 0)
    42.         {
    43.             EndDialogue();
    44.             return;
    45.  
    46.            
    47.  
    48.         }
    49.         string sentance = sentances.Dequeue();
    50.         dialoguetext.text = sentance;
    51.  
    52.     }
    53.     public void EndDialogue()
    54.     {
    55.         Debug.Log("end of conversation");
    56.  
    57.  
    58.     }
    59.  
    60. }
    61.  
     
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    241
    In your startdialogue method, where the foreach loop is, on each iteration, you execute the displayNextSentence method (line 29), thus flipping through the dialogue immediately upon initializing it. Just try moving line 29 after the loop so that this command executes only once.
     
    bernardwalrus44 and Nad_B like this.
  3. bernardwalrus44

    bernardwalrus44

    Joined:
    Feb 25, 2024
    Posts:
    2
    this worked great! thanks.