Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Weird Bug with Dialog System

Discussion in 'UGUI & TextMesh Pro' started by austincjcain, Sep 19, 2023.

  1. austincjcain

    austincjcain

    Joined:
    Aug 25, 2023
    Posts:
    1
    I am experiencing a strange bug with my dialog system. The first two lines of dialog are duplicated and the characters name is not shown.

    This is the first line presented to the player as soon at they approach the NPC.
    upload_2023-9-19_15-18-34.jpeg

    When the player presses left click to advance to the next line, it only adds a space to the existing line going from "Greetings!" to " Greetings!".

    Upon clicking again (at this point 3 lines in), it correctly displays the second line of text. This is the correct format for the text. All other lines after this point are correct, which continues until the end of the file is read.
    upload_2023-9-19_15-19-41.jpeg

    Any help at all is appreciated.


    Here is the code for the character, and the dialog system:
    Code (CSharp):
    1. // Citation: Code heavily inspired by: https://www.youtube.com/watch?v=8oTYabhj248
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using TMPro;
    7. using System.IO;
    8. using System.IO.IsolatedStorage;
    9. using System.Linq;
    10.  
    11. public class Dialogue : MonoBehaviour
    12. {
    13.     [SerializeField] TextMeshProUGUI dialogTextDisplay;
    14.     [SerializeField] TextMeshProUGUI nameTextDisplay;
    15.     [SerializeField] float textSpeed;
    16.     private string[] lines;
    17.     private string[] names;
    18.     private int index;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         // Debug.Log(textFile.text);
    24.         dialogTextDisplay.text = string.Empty;
    25.         nameTextDisplay.text = string.Empty;
    26.         index = 0;
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         // Allow player to skip typing animation through input
    33.         if ( Input.GetMouseButtonDown(0) )
    34.         {
    35.             if ( dialogTextDisplay.text == lines[index])
    36.             {
    37.                 nextLine();
    38.             } else {
    39.                 StopAllCoroutines();
    40.                 dialogTextDisplay.text = lines[index];
    41.             }
    42.         }
    43.     }
    44.  
    45.     public void startDialogue(TextAsset dialog)
    46.     {
    47.         this.gameObject.SetActive(true);
    48.         loadDialog(dialog);
    49.         index = 0;
    50.         StartCoroutine( typeLine() );
    51.     }
    52.  
    53.     // Type out next line character by character
    54.     private IEnumerator typeLine()
    55.     {
    56.         nameTextDisplay.text = names[index];
    57.         foreach ( char c in lines[index].ToCharArray() )
    58.         {
    59.             dialogTextDisplay.text += c;
    60.             yield return new WaitForSeconds(textSpeed);
    61.         }
    62.     }
    63.  
    64.     private void nextLine()
    65.     {
    66.         if ( index < lines.Length - 1 ) {
    67.             index++;
    68.             dialogTextDisplay.text = string.Empty;
    69.             nameTextDisplay.text = string.Empty;
    70.             StartCoroutine( typeLine() );
    71.         } else {
    72.             index = 0;
    73.             gameObject.SetActive( false );
    74.         }
    75.  
    76.     }
    77.  
    78.     // Initialize Lines Array from File
    79.     private void loadDialog(TextAsset inFile)
    80.     {
    81.         // Split Dialog File by Line Breaks
    82.         lines = inFile.text.Split(new char[] { '\r', '\n' },
    83.             System.StringSplitOptions.RemoveEmptyEntries);
    84.         names = new string[lines.Length];
    85.  
    86.         // Separate Lines and Names
    87.         int i = 0;
    88.         foreach ( string s in lines)
    89.         {
    90.             names[i] = s.Split(":").First();
    91.             lines[i] = s.Split(":").Last();
    92.             i++;
    93.         }
    94.  
    95.     }
    96. }
    97.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LeviStartDialogue : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] Dialogue dialogueObject;
    9.     [SerializeField] TextAsset leviDialog;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         leviDialog = Resources.Load("LeviDialog1") as TextAsset;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.     }
    22.  
    23.     private void OnTriggerEnter2D(Collider2D collision)
    24.     {
    25.         if ( collision.tag == "Player" )
    26.         {
    27.             Debug.Log("HI LEVI!");
    28.             dialogueObject.startDialogue(leviDialog);
    29.         }
    30.     }
    31. }
    32.