Search Unity

Need Help With This Script's Logic

Discussion in 'Scripting' started by JoshMBeyer, Dec 19, 2014.

  1. JoshMBeyer

    JoshMBeyer

    Joined:
    Aug 20, 2012
    Posts:
    48
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Topic_01 : MonoBehaviour {
    7.  
    8.     public GameObject ClickedButton;
    9.     public bool Active = true;
    10.     //Wait methods..
    11.     IEnumerator CorrectPause()
    12.     {
    13.        ClickedButton.GetComponentInChildren<Text>().color = Color.green;
    14.        Active = false;
    15.         yield return new WaitForSeconds(2.5f);
    16.         Active = true;
    17.         ClickedButton.GetComponentInChildren<Text>().color = Color.black;
    18.         questionList.Remove(questionList[questionIndex]);
    19.  
    20.         if (questionList.Count > 0)
    21.         {
    22.             Question question = GetQuestion();
    23.             displayQuestion(question);
    24.         }
    25.         else
    26.         {
    27.             //Here we would add the Ending...
    28.             Debug.Log("Out Of Questions To Display");
    29.             ChangeScene.ChangeToSceneStatic("LevelSelect");
    30.         }
    31.     }
    32.     IEnumerator IncorrectPause()
    33.     {
    34.         ClickedButton.GetComponentInChildren<Text>().color = Color.red;
    35.         Active = false;
    36.         yield return new WaitForSeconds(2.5f);
    37.         ClickedButton.GetComponentInChildren<Text>().color = Color.black;
    38.         Active = true;
    39.         Question question = GetQuestion();
    40.         displayQuestion(question);
    41.     }
    42.  
    43. //    public int questionTextSize;
    44. //    public int buttonTextSize;
    45. //    public int buttonWidth;
    46. //    public int buttonHeight;
    47.     public int questionIndex;
    48.    
    49.     class Question
    50.     {
    51.         public string QuestionText;
    52.         public List<string> Answers = new List<string>();
    53.         public int CorrectAnswer;
    54.     }
    55.    
    56.     List<Question> questionList = new List<Question>();
    57.    
    58.     Question GetQuestion()
    59.     {
    60.         int rand = Random.Range (0, questionList.Count);
    61.         questionIndex = rand;
    62.         Debug.Log ("Current Question: " + questionIndex);
    63.         return questionList [rand];
    64.     }
    65.  
    66.     const int maxNumberOfAnswers = 6; // number of question buttons
    67.     GameObject [] answerButton = new GameObject [maxNumberOfAnswers];
    68.     Text [] answerButtonText = new Text   [maxNumberOfAnswers];
    69.     Text questionText;
    70.  
    71.  
    72.  
    73.     //--------------------------------------------------------
    74.     void Awake()
    75.     //----------------------------------------------------------
    76.     {
    77.        
    78.         Active = true;
    79.  
    80.         Question question = new Question ();
    81.         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?";
    82. /*Answer 0*/    question.Answers.Add("174/69 Men:125/64 Women");
    83. /*Answer 1*/    question.Answers.Add("154/69 Men:137/64 Women");
    84. /*Answer 2*/    question.Answers.Add("117/69 Men:136/64 Women");
    85. /*Answer 3*/    question.Answers.Add("174/69 Men:136/64 Women");
    86.         question.CorrectAnswer = 3;
    87.         questionList.Add (question);
    88.  
    89.         question = new Question ();
    90.         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?";
    91.         question.Answers.Add("15 to 30 percent");
    92.         question.Answers.Add("5 to 7 percent");
    93.         question.Answers.Add("5 to 10 percen");
    94.         question.Answers.Add("10 to 15 percent");
    95.         question.CorrectAnswer = 2;
    96.         questionList.Add (question);
    97.  
    98.         question = new Question ();
    99.         question.QuestionText = "What is a unit of measure for mass equal to 0.035 ounce?";
    100.         question.Answers.Add("Gram (g)");
    101.         question.Answers.Add("Kilo (K)");
    102.         question.Answers.Add("Calorie (c)");
    103.         question.Answers.Add("Ounce (oz)");
    104.         question.CorrectAnswer = 0;
    105.         questionList.Add (question);
    106.  
    107.     } // ~Awake
    108.  
    109.  
    110.  
    111.     //-----------------------------------------------------------------
    112.     void Start () {
    113.     //-----------------------------------------------------------------
    114.         // Setting button font size relativ to screen width
    115.         // to make it independent of creen resolution.
    116.         int buttonFontSize = Screen.width / 30;
    117.  
    118.         // We need to find some object to control them at run time,
    119.         // turnin them on or off and change texts.
    120.  
    121.         for (int i = 0; i < maxNumberOfAnswers; ++i)
    122.         {
    123.             SceneUtils.tryToFindMandatoryNamedObject (
    124.                 ref answerButton [i], "Answer " + i + " Button");
    125.  
    126.             SceneUtils.tryToFindMandatoryNamedObjectComponent<Text> (
    127.                 ref answerButtonText [i], "Answer " + i + " Button Text");
    128.  
    129.             answerButtonText [i].fontSize = buttonFontSize;
    130.         }
    131.  
    132.         SceneUtils.tryToFindMandatoryNamedObjectComponent<Text> (
    133.             ref questionText, "Question Text");
    134.  
    135.         // Setting question text font size relativ to screen width
    136.         // to make it independent of creen resolution.
    137.         questionText.fontSize = Screen.width / 25;
    138.  
    139.         // Setting first question.
    140.         Question question = GetQuestion ();
    141.         displayQuestion (question);
    142.     } // ~Start
    143.  
    144.  
    145.  
    146.     //------------------------------------------------------
    147.     void displayQuestion (Question question) {
    148.     //------------------------------------------------------
    149.         questionText.text = question.QuestionText;
    150.         //Track the movement of the correct answer;
    151.         //Make the correct equal the new Answer button;
    152.         //question.CorrectAnswer =
    153.         Debug.Log("The Correct Answer is: " + question.Answers[question.CorrectAnswer].ToString());
    154.         Debug.Log(question.CorrectAnswer);
    155.         // Variable answers, as well as answerCount,
    156.         // introduced for faster access fo some data.
    157.         List<string> answers = question.Answers;
    158.         int i, answerCount = answers.Count;
    159.  
    160.         if (answerCount > maxNumberOfAnswers)
    161.             Debug.Log ("answerCount is greater then maxNumberOfAnswers");
    162.  
    163.         // First, we must enable necessery number of
    164.         // buttons and display answer text for them.
    165.  
    166.         //Randomize the answer display order..
    167.         for (i = 0; i < answerCount; ++i)
    168.         {
    169.             answerButton[i].SetActive(true);
    170.             int selectedAnswer = Random.Range(0, answerCount - 1 - i);
    171.             answerButtonText[i].text = answers[selectedAnswer];
    172.             answers.RemoveAt(selectedAnswer);
    173.  
    174.         }
    175.         question.CorrectAnswer = question.Answers.IndexOf("Wherever the correct answer is at now");
    176.        
    177.         // Then we should hide extra buttons, which is
    178.         // not containig answers.
    179.         for (i = answerCount; i < maxNumberOfAnswers; ++i)
    180.         {
    181.             answerButton [i].SetActive (false);
    182.         }
    183.     } // ~displayQuestion
    184.  
    185.  
    186.  
    187.     //-----------------------------------------------------------------
    188.     public void on_AnswerButton_Click (int buttonIndex) {
    189.     //-----------------------------------------------------------------
    190.         if (Active)
    191.         {
    192.  
    193.             ClickedButton = GameObject.Find("Answer " + buttonIndex + " Button");
    194.  
    195.             if (buttonIndex == questionList[questionIndex].CorrectAnswer)
    196.             {
    197.                 //If correct answer...
    198.                 Debug.Log("Your Answer Was Correct!");
    199.                 StartCoroutine(CorrectPause());
    200.  
    201.             }
    202.             else
    203.             {
    204.                 Debug.Log("Your Answer Was Incorrect!");
    205.                 //if incorrect....
    206.                 StartCoroutine(IncorrectPause());
    207.  
    208.             }
    209.         }
    210.     } // ~on_AnswerButton_Click
    211.  
    212. }
    213.  
     

    Attached Files:

    Last edited: Dec 19, 2014
  2. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    Off the top of my head, the easiest thing to do that fits with your code is to have an array of ints that represents the index of the answer of the list. Example:

    Code (CSharp):
    1. buttonToAnswers[buttonIndex] = AnswerIndex
    EDIT:
    For your second question, a quick way is to a list for the questions remaining. Get a random index for the question you want. Example:

    Code (CSharp):
    1. List<int> Unanswered = new List<int>();
    2. //Add index of unanswered questions
    3.  
    4. currentQuestion = UnansweredQuestion[Random.Range(0, Unanswered.Count);
    5.  
    6. //Remove when finished
     
    Last edited: Dec 19, 2014