Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Quiz Game - Repeating all wrong answered questions

Discussion in 'Scripting' started by ney0, Jul 12, 2019.

  1. ney0

    ney0

    Joined:
    Dec 26, 2018
    Posts:
    9
    Hey there,

    this is my first thread I hope I won't miss anything. I'm a Unity and C# beginner and currently I'm trying to develop a Quiz Game. For test purposes there are only 2 questions stored in a json File. I want to repeat the wrong answered questions. They should be displayed after the last question of a list, but I just cant find a proper way to do that. So far I could manage to display the questions but when answering the last wrong answered question I get the error: "Index was out of range. Must be non-negative and less than the size of the collection."
    I would be grateful for any help :)

    Code (CSharp):
    1.  
    2.    
    3.     private void ShowQuestion()
    4.     {
    5.         RemoveAnswerButtons();
    6.  
    7.         poolList = new List<Question_Script>(questionPool);
    8.         Question_Script questionData = poolList[questionIndex];
    9.         questionDisplayText.text = questionData.question;
    10.  
    11.         for (int i = 0; i < questionData.answers.Length; i++)
    12.         {
    13.             GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
    14.             answerButtonGameObjects.Add(answerButtonGameObject);
    15.             answerButtonGameObject.transform.SetParent(answerButtonParent);
    16.  
    17.             AnswerButton_Script answerButton = answerButtonGameObject.GetComponent<AnswerButton_Script>();
    18.             answerButton.Setup(questionData.answers[i]);
    19.         }
    20.     }
    21.  
    22.     private void Show()
    23.     {
    24.         RemoveAnswerButtons();
    25.  
    26.         questionDisplayText.text = fbF.question;
    27.  
    28.         for (int i = 0; i < fbF.answers.Length; i++)
    29.         {
    30.             GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
    31.             answerButtonGameObjects.Add(answerButtonGameObject);
    32.             answerButtonGameObject.transform.SetParent(answerButtonParent);
    33.  
    34.             AnswerButton_Script answerButton = answerButtonGameObject.GetComponent<AnswerButton_Script>();
    35.             answerButton.Setup(fbF.answers[i]);
    36.         }
    37.     }
    38.  
    39.     private void RemoveAnswerButtons()
    40.     {
    41.         while(answerButtonGameObjects.Count> 0)
    42.         {
    43.             answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
    44.             answerButtonGameObjects.RemoveAt(0);
    45.         }
    46.     }
    47.  
    48.     public void AnswerButtonClicked(bool correct)
    49.     {
    50.         if (correct)
    51.         {
    52.             playerScore += currentRoundData.addScore;
    53.             scoreDisplayText.text = "Score" + playerScore.ToString();
    54.             poolList.RemoveAt(0);
    55.         }
    56.  
    57.         else
    58.         {
    59.             allfbF.Add(poolList[questionIndex]);
    60.             poolList.RemoveAt(0);
    61.         }
    62.  
    63.         if (poolList.Count > questionIndex)
    64.         {
    65.             questionIndex++;
    66.             ShowQuestion();
    67.          }
    68.  
    69.         else
    70.         {
    71.             if (allfbF.Count > 0 && poolList.Count <= questionIndex)
    72.             {
    73.                 Show();
    74.                 if (allfbF.Count > fbFIndex)
    75.                 {
    76.                     if (correct)
    77.                     {
    78.                         allfbF.RemoveAt(0);
    79.                         fbFIndex++;
    80.                     }
    81.                 }
    82.                 else
    83.                 {
    84.                     EndRound();
    85.                 }
    86.             }
    87.         }
    88.     }
    89.  
     
unityunity