Its a multiple choice question. There are a few possible answers, but only one of them is correct. Each answer is displayed as a unity 4.6 Button. If the selected answer is correct, remove that question from the list, and get a new question. If the selected answer is wrong, just get a new question. The answers are displayed in a random order every time, that way the user cannot remember which Button, it is, instead of answer. I have it almost complete but there are some issues. I know it has to do with the way I am randomizing the answer’s but can’t figure it out, i’ve tried two methods, and they work sometimes, then next thing I know the answer i choose is correct, but it says its not. Can anyone see the solution to this?
And also how could I make it to where it stores the last question asked so it’s impossible to ask the same question twice. (Unless its the only question left ofcourse) but this is less important. I just need to get it working properly first. Here is the script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Topic_01 : MonoBehaviour {
public GameObject ClickedButton;
public bool Active = true;
//Wait methods..
IEnumerator CorrectPause()
{
ClickedButton.GetComponentInChildren<Text>().color = Color.green;
Active = false;
yield return new WaitForSeconds(2.5f);
Active = true;
ClickedButton.GetComponentInChildren<Text>().color = Color.black;
questionList.Remove(questionList[questionIndex]);
if (questionList.Count > 0)
{
Question question = GetQuestion();
displayQuestion(question);
}
else
{
//Here we would add the Ending...
Debug.Log("Out Of Questions To Display");
ChangeScene.ChangeToSceneStatic("LevelSelect");
}
}
IEnumerator IncorrectPause()
{
ClickedButton.GetComponentInChildren<Text>().color = Color.red;
Active = false;
yield return new WaitForSeconds(2.5f);
ClickedButton.GetComponentInChildren<Text>().color = Color.black;
Active = true;
Question question = GetQuestion();
displayQuestion(question);
}
// public int questionTextSize;
// public int buttonTextSize;
// public int buttonWidth;
// public int buttonHeight;
public int questionIndex;
class Question
{
public string QuestionText;
public List<string> Answers = new List<string>();
public int CorrectAnswer;
}
List<Question> questionList = new List<Question>();
Question GetQuestion()
{
int rand = Random.Range (0, questionList.Count);
questionIndex = rand;
Debug.Log ("Current Question: " + questionIndex);
return questionList [rand];
}
const int maxNumberOfAnswers = 6; // number of question buttons
GameObject [] answerButton = new GameObject [maxNumberOfAnswers];
Text [] answerButtonText = new Text [maxNumberOfAnswers];
Text questionText;
//--------------------------------------------------------
void Awake()
//----------------------------------------------------------
{
Active = true;
Question question = new Question ();
question.QuestionText = "Body size. Reference measures for weight and height of military members are how many pounds and how many inches for men, and how many pounds and how many inches for women?";
/*Answer 0*/ question.Answers.Add("174/69 Men:125/64 Women");
/*Answer 1*/ question.Answers.Add("154/69 Men:137/64 Women");
/*Answer 2*/ question.Answers.Add("117/69 Men:136/64 Women");
/*Answer 3*/ question.Answers.Add("174/69 Men:136/64 Women");
question.CorrectAnswer = 3;
questionList.Add (question);
question = new Question ();
question.QuestionText = "Work in severe cold may result in very high energy requirements. Even mildly cold temperatures (32 to 57 degrees F) can increase energy requirements by what percentage?";
question.Answers.Add("15 to 30 percent");
question.Answers.Add("5 to 7 percent");
question.Answers.Add("5 to 10 percen");
question.Answers.Add("10 to 15 percent");
question.CorrectAnswer = 2;
questionList.Add (question);
question = new Question ();
question.QuestionText = "What is a unit of measure for mass equal to 0.035 ounce?";
question.Answers.Add("Gram (g)");
question.Answers.Add("Kilo (K)");
question.Answers.Add("Calorie (c)");
question.Answers.Add("Ounce (oz)");
question.CorrectAnswer = 0;
questionList.Add (question);
} // ~Awake
//-----------------------------------------------------------------
void Start () {
//-----------------------------------------------------------------
// Setting button font size relativ to screen width
// to make it independent of creen resolution.
int buttonFontSize = Screen.width / 30;
// We need to find some object to control them at run time,
// turnin them on or off and change texts.
for (int i = 0; i < maxNumberOfAnswers; ++i)
{
SceneUtils.tryToFindMandatoryNamedObject (
ref answerButton *, "Answer " + i + " Button");*
-
SceneUtils.tryToFindMandatoryNamedObjectComponent<Text> (*
_ ref answerButtonText , “Answer " + i + " Button Text”);_
_ answerButtonText .fontSize = buttonFontSize;
* }*_
* SceneUtils.tryToFindMandatoryNamedObjectComponent (*
* ref questionText, “Question Text”);*
* // Setting question text font size relativ to screen width*
* // to make it independent of creen resolution.*
* questionText.fontSize = Screen.width / 25;*
* // Setting first question.*
* Question question = GetQuestion ();*
* displayQuestion (question);*
* } // ~Start*
* //------------------------------------------------------*
* void displayQuestion (Question question) {*
* //------------------------------------------------------*
* questionText.text = question.QuestionText;*
//Track the movement of the correct answer;
//Make the correct equal the new Answer button;
//question.CorrectAnswer =
Debug.Log("The Correct Answer is: " + question.Answers[question.CorrectAnswer].ToString());
Debug.Log(question.CorrectAnswer);
* // Variable answers, as well as answerCount,*
* // introduced for faster access fo some data.*
* List answers = question.Answers;*
* int i, answerCount = answers.Count;*
* if (answerCount > maxNumberOfAnswers)*
* Debug.Log (“answerCount is greater then maxNumberOfAnswers”);*
* // First, we must enable necessery number of*
* // buttons and display answer text for them.*
//Randomize the answer display order…
for (i = 0; i < answerCount; ++i)
{
answerButton*.SetActive(true);*
int selectedAnswer = Random.Range(0, answerCount - 1 - i);
answerButtonText*.text = answers[selectedAnswer];*
answers.RemoveAt(selectedAnswer);
}
question.CorrectAnswer = question.Answers.IndexOf(“Wherever the correct answer is at now”);
* // Then we should hide extra buttons, which is*
* // not containig answers.*
* for (i = answerCount; i < maxNumberOfAnswers; ++i)*
* {*
_ answerButton .SetActive (false);
* }
} // ~displayQuestion*_
* //-----------------------------------------------------------------*
* public void on_AnswerButton_Click (int buttonIndex) {
_ //-----------------------------------------------------------------
if (Active)
{*_
ClickedButton = GameObject.Find(“Answer " + buttonIndex + " Button”);
if (buttonIndex == questionList[questionIndex].CorrectAnswer)
{
//If correct answer…
Debug.Log(“Your Answer Was Correct!”);
StartCoroutine(CorrectPause());
}
else
{
Debug.Log(“Your Answer Was Incorrect!”);
//if incorrect…
StartCoroutine(IncorrectPause());
}
}
* } // ~on_AnswerButton_Click*
}