Search Unity

Bug Dialogue System isn't working as intended

Discussion in 'Scripting' started by Draugrlord, May 26, 2023.

  1. Draugrlord

    Draugrlord

    Joined:
    Oct 1, 2021
    Posts:
    30
    Apologies for incoming massive text wall but wanted to give as much information as possible.

    Not sure if this is the right forum to be in for this issue. I am working on making a 2D RPG game and I've been using a course I found on Skillshare to create my game. I have been in contact with the teacher about this bug but even he is stumped on why this isn't working.

    The bug I am running into is that when I go to initiate dialogue with an NPC, it triggers fine but the game doesn't recognize I have hit the key to continue the dialogue until I hit the key a second time which then puts the dialogue into an endless loop. So, basically, I hit "E" to trigger dialogue, first box comes up but then I have to hit "E" twice to go to the next line of dialogue. It is just this first line that does this, no other parts of the dialogue cause this bug to occur.

    The coding is exactly like how it is presented in the course on Skillshare I even rewatched those videos to double check. When I first created the dialogue, it worked without any issues. Due to work becoming busy I had to take about a month break and when returning to work on the game, I began running into the bug mentioned above. Unity had gone through some updates and the teacher had said he had other students also encounter this as well as they updated Unity, but others seem unaffected by this.

    I did back up Unity to a previous version when the dialogue was working but even doing that the bug persisted. I'm posting the code that has been created for the dialogue system. I have been using debug logs at the different intervals for triggering dialogue, moving to the next sentence and then ending the conversation which automatically closes the dialogue box. Every debug log triggers exactly where it should in the console except for the beginning where it takes hitting the key twice for dialogue to continue which causes the endless loop. I have deleted the dialogue component from the NPC and replaced it with a new dialogue component, I've completely rewritten the dialogue, applied the dialogue onto entirely different NPCs, everything still triggers the exact same bug.

    Now I have tested it and it seems when I transition to a new scene then back to the original scene it fixes the dialogue bug, and it works fine. Knowing this I did go ahead and move the NPC to the second scene to where the transition occurs before dialogue is triggered but that doesn't fix the bug, I still get hit with the endless loop bug even if he is in the second scene.

    From what I can tell it seems to be possibly an input error? I've used different keys and even the mouse to trigger the dialogue all with the same results mentioned above.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DialogueHandler : MonoBehaviour
    6. {
    7.     public string[] sentences;
    8.     private bool canActivateBox;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (canActivateBox && Input.GetKeyUp(KeyCode.E) && !DialogueController.instance.IsDialogueBoxActive())
    20.         {
    21.             DialogueController.instance.ActivateDialogue(sentences);
    22.             Debug.Log("Conversation started");
    23.         }
    24.     }
    25.  
    26.     private void OnTriggerEnter2D(Collider2D collision)
    27.     {
    28.         if (collision.CompareTag("Player"))
    29.         {
    30.             canActivateBox = true;
    31.         }
    32.     }
    33.  
    34.     private void OnTriggerExit2D(Collider2D collision)
    35.     {
    36.         if (collision.CompareTag("Player"))
    37.         {
    38.             canActivateBox = false;
    39.         }
    40.     }
    41. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class DialogueController : MonoBehaviour
    8. {
    9.     [SerializeField] TextMeshProUGUI dialogueText, nameText;
    10.     [SerializeField] GameObject dialogueBox, nameBox;
    11.     public string[] dialogueLines;
    12.  
    13.     [SerializeField] int currentSentence;
    14.  
    15.     public static DialogueController instance;
    16.  
    17.     public bool dialogueJustStarted;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         instance = this;
    23.  
    24.         dialogueText.text = dialogueLines[currentSentence];
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         if (dialogueBox.activeInHierarchy)
    31.         {
    32.             if (Input.GetKeyUp(KeyCode.E))
    33.             {
    34.                 if (!dialogueJustStarted)
    35.                 {
    36.                     currentSentence++;
    37.                     Debug.Log("Next Sentence");
    38.  
    39.                     if (currentSentence >= dialogueLines.Length)
    40.                     {
    41.                         dialogueBox.SetActive(false);
    42.                         GameManager.instance.dialogueBoxOpened = false;
    43.                         Debug.Log("End conversation");
    44.                     }
    45.  
    46.                     else
    47.                     {
    48.                         CheckForName();
    49.                         dialogueText.text = dialogueLines[currentSentence];
    50.                     }
    51.                 }
    52.  
    53.                 else
    54.                 {
    55.                     dialogueJustStarted = false;
    56.                 }
    57.  
    58.             }
    59.            
    60.         }      
    61.     }
    62.  
    63.     public void ActivateDialogue(string[] newSentencesToUse)
    64.     {
    65.         dialogueLines = newSentencesToUse;
    66.         currentSentence = 0;
    67.  
    68.         CheckForName();
    69.         dialogueText.text = dialogueLines[currentSentence];
    70.         dialogueBox.SetActive(true);
    71.  
    72.         dialogueJustStarted = true;
    73.         GameManager.instance.dialogueBoxOpened = true;
    74.     }
    75.  
    76.     void CheckForName()
    77.     {
    78.         if (dialogueLines[currentSentence].StartsWith("#"))
    79.         {
    80.             nameText.text = dialogueLines[currentSentence].Replace("#", "");
    81.             currentSentence++;
    82.         }
    83.     }
    84.  
    85.     public bool IsDialogueBoxActive()
    86.     {
    87.         return dialogueBox.activeInHierarchy;
    88.     }
    89. }
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    In the first script change Update to LateUpdate and check if this help.
     
  3. Draugrlord

    Draugrlord

    Joined:
    Oct 1, 2021
    Posts:
    30
    Can't believe I forgot about LateUpdate, that worked. I put it on the first script for the handler and that didn't fix the issue but I put it on the second script for the controller, it fixed the bug. Even closed Unity down and restarted it to make sure if wasn't a fluke and low and behold the bug is still gone. Thank you so much for this fix AngryProgrammer, you're awesome!
     
  4. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    No problem. I notice that you make similar key check in two different places, and problem is that you don't have guarantee in what order they will be executed (they both equal if using Update).

    Edit: At a later time try to redesign this so that a key check and logic are executed in one place.
     
    mopthrow likes this.
  5. Draugrlord

    Draugrlord

    Joined:
    Oct 1, 2021
    Posts:
    30
    I get what you're saying because the dialogue starting and the continuing of the dialogue both have the same trigger they kept firing at the same time, almost like Unity flipping a coin and then deciding what to execute. That makes a lot of sense now and definitely making a note for future iterations of this type of bug to make sure nothing is overlapping like that.
     
    mopthrow likes this.