Queue emptying by itself
I am having trouble figuring out a bug. My game initializes several queues to store the game prompts, and in the "Start" button, the function "StartDialogue()" fills each of the queues and goes to "DisplayNext()". This function is supposed to dequeue the first prompt and put them into the dialogue boxes. Afterwards, the player selects the correct button to "DisplayNext()" to advance to the next prompt.
However, my problem is that my "sen_queue" count is setting itself to 0 when the correct button calls "DisplayNext()". The debug output first displays 4 when "Start" is pressed, but when the correct button is pressed, it displays 0. With the first correct button press, it goes to the "EndDialogue()" function. I have attached my code below. I have no clue how to fix this so any help would be greatly appreciated! Thank you so much in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuestionManager : MonoBehaviour
{
public Text dialogueText;
public Text topText;
public Text botText;
public Queue<string> sen_queue;
public Queue<string> top_queue;
public Queue<string> bot_queue;
public Queue<string> wrong_queue;
private bool prev_wrong;
public GameObject finalsaltwater;
public GameObject water;
public GameObject sand;
public GameObject salt;
public GameObject filtersand;
public GameObject platewater;
void Start()
{
Screen.SetResolution(1600, 900, true);
sen_queue = new Queue<string>();
top_queue = new Queue<string>();
bot_queue = new Queue<string>();
wrong_queue = new Queue<string>();
prev_wrong = false;
finalsaltwater.SetActive(false);
filtersand.SetActive(false);
platewater.SetActive(false);
}
public void StartDialogue(Question question, Question topprompt, Question botprompt, Question wrong)
{
sen_queue.Clear();
top_queue.Clear();
bot_queue.Clear();
wrong_queue.Clear();
foreach (string sentence in question.sentences)
{
sen_queue.Enqueue(sentence);
}
foreach (string sentence in topprompt.sentences)
{
top_queue.Enqueue(sentence);
}
foreach (string sentence in botprompt.sentences)
{
bot_queue.Enqueue(sentence);
}
foreach (string sentence in wrong.sentences)
{
wrong_queue.Enqueue(sentence);
}
DisplayNext();
}
IEnumerator ToggleFalse(GameObject thing)
{
yield return new WaitForSeconds(1.5f);
thing.SetActive(false);
}
IEnumerator ToggleTrue(GameObject thing)
{
yield return new WaitForSeconds(1.5f);
thing.SetActive(true);
}
public void DisplayNext()
{
Debug.Log("IN DISPLAY " + sen_queue.Count);
if (sen_queue.Count == 0)
{
EndDialogue();
return;
}
if (top_queue.Count == 2)
{
platewater.SetActive(true);
water.SetActive(false);
StartCoroutine(ToggleFalse(salt));
} else if (top_queue.Count == 1)
{
filtersand.SetActive(true);
sand.SetActive(false);
platewater.SetActive(false);
StartCoroutine(ToggleTrue(finalsaltwater));
} else if (top_queue.Count == 0)
{
platewater.SetActive(true);
StartCoroutine(ToggleTrue(salt));
StartCoroutine(ToggleFalse(platewater));
finalsaltwater.SetActive(false);
}
string sentence = sen_queue.Dequeue();
if (top_queue.Count != 0)
{
string topbutton = top_queue.Dequeue();
topText.text = topbutton;
}
if (bot_queue.Count != 0)
{
string botbutton = bot_queue.Dequeue();
botText.text = botbutton;
}
prev_wrong = false;
dialogueText.text = sentence;
}
public void IncorrectDisplay()
{
if (wrong_queue.Count == 0)
{
return;
}
if (prev_wrong == false)
{
string wrong_sen = wrong_queue.Dequeue();
dialogueText.text = wrong_sen;
}
prev_wrong = true;
}
public void EndDialogue()
{
dialogueText.text = "Good job! Refresh the page if you want to restart.";
return;
}
}
Your answer
Follow this Question
Related Questions
How To input text from a button? 1 Answer
Changing the image of a button with a script 1 Answer
Remove button OnMouseOver highlighting 0 Answers
UI buttons won't recognize script 0 Answers
Button Displacement 0 Answers