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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueHandler : MonoBehaviour
{
public string[] sentences;
private bool canActivateBox;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (canActivateBox && Input.GetKeyUp(KeyCode.E) && !DialogueController.instance.IsDialogueBoxActive())
{
DialogueController.instance.ActivateDialogue(sentences);
Debug.Log("Conversation started");
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
canActivateBox = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
canActivateBox = false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueController : MonoBehaviour
{
[SerializeField] TextMeshProUGUI dialogueText, nameText;
[SerializeField] GameObject dialogueBox, nameBox;
public string[] dialogueLines;
[SerializeField] int currentSentence;
public static DialogueController instance;
public bool dialogueJustStarted;
// Start is called before the first frame update
void Start()
{
instance = this;
dialogueText.text = dialogueLines[currentSentence];
}
// Update is called once per frame
void Update()
{
if (dialogueBox.activeInHierarchy)
{
if (Input.GetKeyUp(KeyCode.E))
{
if (!dialogueJustStarted)
{
currentSentence++;
Debug.Log("Next Sentence");
if (currentSentence >= dialogueLines.Length)
{
dialogueBox.SetActive(false);
GameManager.instance.dialogueBoxOpened = false;
Debug.Log("End conversation");
}
else
{
CheckForName();
dialogueText.text = dialogueLines[currentSentence];
}
}
else
{
dialogueJustStarted = false;
}
}
}
}
public void ActivateDialogue(string[] newSentencesToUse)
{
dialogueLines = newSentencesToUse;
currentSentence = 0;
CheckForName();
dialogueText.text = dialogueLines[currentSentence];
dialogueBox.SetActive(true);
dialogueJustStarted = true;
GameManager.instance.dialogueBoxOpened = true;
}
void CheckForName()
{
if (dialogueLines[currentSentence].StartsWith("#"))
{
nameText.text = dialogueLines[currentSentence].Replace("#", "");
currentSentence++;
}
}
public bool IsDialogueBoxActive()
{
return dialogueBox.activeInHierarchy;
}
}