- Home /
Before I waist a ton of time... Need help
So I am making a questionnaire. There is 1 question and 4 possible answers. Only one answer is correct however. I will have over 1000 questions and over 4000 answers. If the user chooses the correct answer I don't want that question to be asked again.. If he chooses the wrong answer, then move to the next question randomly but leaving the last question in the pile to be asked again later on. So this is how I am going about it so far. But before I go any further I feel there is a MUCH better way of doing this and will save me some time/efficiency. Any advice is appreciated...
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class QAndA : MonoBehaviour {
public List<string> Questions;
public List<string> Answers;
public List<string> IncorrectAnswers;
int question = 0;
public bool correct;
void Awake()
{
//-------------Questions-----------------//
Questions[0] = "What is 2+2?";
Questions[1] = "What is 5+5?";
//-------------Answers-------------------//
Answers[0] = "4";
Answers[1] = "10";
//-------------Incorrect Answers---------//
IncorrectAnswers[0] = "22";
IncorrectAnswers[1] = "2";
IncorrectAnswers[2] = "44";
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
switch (question)
{
case 0:
GUI.Label(new Rect(25, 25, 125, 50), Questions[0]);
if(GUI.Button(new Rect(125,25,125,50), Answers[0]))
{
//Move to the next question, but allow this question to be asked again
//At a randomly later time.
if (correct) { question = Random.Range(0, Questions.Count); }
}
if (GUI.Button(new Rect(125, 75, 125, 50), IncorrectAnswers[0]))
{
//Move to next question randomly and keep this question from being
//asked again.
Questions.Remove(Questions[0]);
question = Random.Range(0, Questions.Count);
}
break;
case 1:
GUI.Label(new Rect(25, 25, 125, 50), Questions[1]);
if (GUI.Button(new Rect(125, 25, 125, 50), Answers[1]))
{
//Move to next question randomly
if (correct) { question = Random.Range(0, Questions.Count); }
}
break;
case 2:
GUI.Label(new Rect(25, 25, 125, 50), Questions[2]);
if (GUI.Button(new Rect(125, 25, 125, 50), Answers[2]))
{
//Move to next question randomly
if (correct) { question = Random.Range(0, Questions.Count); }
}
break;
case 3:
GUI.Label(new Rect(25, 25, 125, 50), Questions[3]);
if (GUI.Button(new Rect(125, 25, 125, 50), Answers[3]))
{
//Move to next question randomly
if (correct) { question = Random.Range(0, Questions.Count); }
}
break;
case 4:
GUI.Label(new Rect(25, 25, 125, 50), Questions[4]);
if (GUI.Button(new Rect(125, 25, 125, 50), Answers[4]))
{
//Move to next question randomly
if (correct) { question = Random.Range(0, Questions.Count); }
}
break;
}
}
}
I found some advice and this seems more appropriate but my questions still remain the same since there will be 1000+ questions and so on.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class QAndA : $$anonymous$$onoBehaviour
{
void Start()
{
var question = new Question();
question.QuestionText = "What color is snow?";
question.Answers[0] = "Red";
question.Answers[1] = "Yellow";
question.Answers[2] = "White";
question.Answers[3] = "Green";
question.CorrectAnswer = 2;
// ... more questions
var listOfQuestions = new List<Question>();
listOfQuestions.Add(question);
}
}
class Question
{
public string QuestionText{ get; set; }
public string AnswerA { get;set; }
public string AnswerB { get;set; }
public string AnswerC { get;set; }
public string AnswerD { get; set; }
public Question()
{
Answers = new string[4];
}
public string[] Answers { get; set; }
public int CorrectAnswer { get; set; }
}
Answer by CodeMasterMike · Dec 10, 2014 at 08:55 AM
Writing all questions in a single script file is not a good design. Its hard to maintain and will be extremly slow and hard to debug.
I think the best way to do this, is to create a database with all your questions and answers and other data you need. Then you can alter the text in the database without having to re-build the game later on.
Then have a script to load a random question from the database at runtime whenever a new question should appear.
Good luck!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Simple Questionnaire Game Logic? 2 Answers
Leaderboard GUISkin? 1 Answer
Raycasting, LineRenderer & Layermasks 0 Answers