Hi,
i want to update this pies of string
Thanks for feed back. back to the drawing board
Hi,
i want to update this pies of string
Thanks for feed back. back to the drawing board
thanks, forgot
I’m not sure what you’re asking. But it appears you were doing a question and then trying to reload the scene, which certainly doesn’t make sense for a trivia game. You should just be calling getRandomQuestion again at the end of your coroutine to load your next question.
But, you may have to add something in to give the player feedback on it being the wrong/right answer and then clear that as well at the end of the coroutine.
For something like this I recommend using event driven approach where user actions progress the game state. For example if user inputs the answer you get an event with string which you can process and act accordingly.
Made a simple QuizBehavior for the fun of it.
It uses Unity Text component and InputField component and hard coded questions with answers. It loads 3 random questions to question pool and once the pool runs out of questions it shows how many of the answers where correct.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuizBehavior : MonoBehaviour
{
public Text QuestionLabel;
public InputField AnswerInput;
public Question[] AllQuestions;
List<Question> UnansweredQuestions = new List<Question>();
List<Question> QuestionPool = new List<Question>();
int correct = 0;
void Start()
{
//Hardcoded. Read and deseralize from file or ScriptableObject instead
AllQuestions = new Question[4];
for (int i = 0; i < AllQuestions.Length; i++)
AllQuestions[i] = new Question();
AllQuestions[0].QuestionMessage = "2 + 2 = 10";
AllQuestions[0].QuestionAnswer = "false";
AllQuestions[1].QuestionMessage = "1 + 1 = 2";
AllQuestions[1].QuestionAnswer = "true";
AllQuestions[2].QuestionMessage = "Red is blue?";
AllQuestions[2].QuestionAnswer = "false";
AllQuestions[3].QuestionMessage = "H2O = Water";
AllQuestions[3].QuestionAnswer = "true";
UnansweredQuestions = new List<Question>(AllQuestions);
AnswerInput.onEndEdit.AddListener(OnAnswer);
BeginQuiz(3);
}
void BeginQuiz(int number)
{
correct = 0;
QuestionPool.Clear();
for (int i = 0; i < number; i++)
{
if (UnansweredQuestions.Count == 0)
break;
int index = Random.Range(0, UnansweredQuestions.Count);
QuestionPool.Add(UnansweredQuestions[index]);
UnansweredQuestions.RemoveAt(index);
}
NextQuestion();
}
void NextQuestion()
{
if (QuestionPool.Count > 0)
{
QuestionLabel.text = QuestionPool[0].QuestionMessage;
AnswerInput.interactable = true;
AnswerInput.text = "";
AnswerInput.ActivateInputField();
}
else
{
QuestionLabel.text = "All done! Correct answers: " + correct;
Invoke("EndQuiz", 2.0f);
}
}
void OnAnswer(string value)
{
if (value == null || value.Trim() == "")
return;
if(QuestionPool.Count > 0)
{
if (QuestionPool[0].QuestionAnswer.ToLower() == value.ToLower().Trim())
{
QuestionLabel.text = "Correct!";
correct += 1;
}
else
{
QuestionLabel.text = "Incorrect!";
}
QuestionPool.RemoveAt(0);
AnswerInput.interactable = false;
Invoke("NextQuestion", 1.0f);
}
else
{
if (value.ToLower() == "yes")
{
BeginQuiz(3);
}
else if (value.ToLower() == "no")
Application.Quit();
}
}
void EndQuiz()
{
if (UnansweredQuestions.Count > 0)
{
QuestionLabel.text = "New game? (yes or no)";
AnswerInput.text = "";
AnswerInput.interactable = true;
AnswerInput.Select();
}
else
{
QuestionLabel.text = "No more questions.";
return;
}
}
}
[System.Serializable]
public class Question
{
public string QuestionMessage = "";
public string QuestionAnswer = "";
}
thank you for the feedback. great to learn form al the imput