Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Oct 30, 2017 at 11:04 AM · quizpool

Getting random question at a maximum of 10 in a pool of question quiz game [help]

So I have followed the tutorial of the Unity Quiz game and wondered that If i want the game to present only up to 10 questions but the pool of questions have 20 or so so this is the script from the tutorial that I have somewhat edited. using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections.Generic;

public class AdditionGameController : MonoBehaviour { public AdditionSimpleObjectPool answerButtonObjectPool; public Text questionText; public Text scoreDisplay; public Text timeRemainingDisplay; public Transform answerButtonParent;

 public GameObject questionDisplay;
 public GameObject roundEndDisplay;
 public Text highScoreDisplay;
 private AdditionDataController dataController;
 private AdditionRoundData currentRoundData;
 private AdditionQuestionData[] questionPool;
 private bool isRoundActive = false;
 private float timeRemaining;
 private int playerScore;
 private int questionIndex;
 private List<GameObject> answerButtonGameObjects = new List<GameObject>();
 private List<int> unselectedIndices= new  List<int> ();

 void Start()
 {
     dataController = FindObjectOfType<AdditionDataController>();                                // Store a reference to the DataController so we can request the data we need for this round
     currentRoundData = dataController.GetCurrentRoundData();                            // Ask the DataController for the data for the current round. At the moment, we only have one round - but we could extend this
     questionPool = currentRoundData.questions; 

     for (int i = 0; i < questionPool.Length; i ++)                                // For every AnswerData in the current QuestionData...
     {
         unselectedIndices.Add(i);
     }
     timeRemaining = currentRoundData.timeLimitInSeconds;                                // Set the time limit for this round based on the RoundData object
     UpdateTimeRemainingDisplay();
     playerScore = 0;
     questionIndex = UnityEngine.Random.Range(0,unselectedIndices.Count);
     ShowQuestion();
     isRoundActive = true;
 }
 void Update()
 {
     if (isRoundActive)
     {
         timeRemaining -= Time.deltaTime;                                                // If the round is active, subtract the time since Update() was last called from timeRemaining
         UpdateTimeRemainingDisplay();
         if (timeRemaining <= 0f)                                                        // If timeRemaining is 0 or less, the round ends
         {
             EndRound();
         }
     }
 }
 void ShowQuestion()
 {
     RemoveAnswerButtons();
     AdditionQuestionData questionData = questionPool[unselectedIndices[questionIndex]];                            // Get the QuestionData for the current question
     questionText.text = questionData.questionText;                                        // Update questionText with the correct text
     for (int i = 0; i < questionData.answers.Length; i ++)                                // For every AnswerData in the current QuestionData...
     {
         GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();            // Spawn an AnswerButton from the object pool
         answerButtonGameObjects.Add(answerButtonGameObject);
         answerButtonGameObject.transform.SetParent(answerButtonParent);
         answerButtonGameObject.transform.localScale = Vector3.one;
         AdditionAnswerButton answerButton = answerButtonGameObject.GetComponent<AdditionAnswerButton>();
         answerButton.SetUp(questionData.answers[i]);                                    // Pass the AnswerData to the AnswerButton so the AnswerButton knows what text to display and whether it is the correct answer
     }
 }
 void RemoveAnswerButtons()
 {
     while (answerButtonGameObjects.Count > 0)                                            // Return all spawned AnswerButtons to the object pool
     {
         answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
         answerButtonGameObjects.RemoveAt(0);
     }
 }
 public void AnswerButtonClicked(bool isCorrect)
 {
     if (isCorrect)
     {
         playerScore += currentRoundData.pointsAddedForCorrectAnswer;                    // If the AnswerButton that was clicked was the correct answer, add points
         scoreDisplay.text = playerScore.ToString();
     }

     unselectedIndices.RemoveAt(questionIndex);
     if(unselectedIndices.Count>0)                                           // If there are more questions, show the next question
     {
         questionIndex= UnityEngine.Random.Range(0,unselectedIndices.Count);
         ShowQuestion();
     }
     else                                                                                // If there are no more questions, the round ends
     {
         EndRound();
     }
 }
 private void UpdateTimeRemainingDisplay()
 {
     timeRemainingDisplay.text = Mathf.Round(timeRemaining).ToString();
 }
 public void EndRound()
 {
     isRoundActive = false;
     questionDisplay.SetActive(false);
     roundEndDisplay.SetActive(true);
 }
 public void ReturnToMenu()
 {
     SceneManager.LoadScene("MenuScreen");
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siaknimarkanthony · Jan 02, 2018 at 05:09 AM 0
Share

did you find the answer for this? i have the same problem too

avatar image thejrz12 siaknimarkanthony · Aug 30, 2018 at 02:26 AM 0
Share

$$anonymous$$E TOO SIR

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ramsdal · Aug 30, 2018 at 01:12 PM

Im not quite sure what you are asking exactly, perhaps because i have not taken this tuturial. Do you just wish the random question selected to be among the first x questions in the pool?

 questionIndex= UnityEngine.Random.Range(0,unselectedIndices.Count);

can then be changed to

 questionIndex= UnityEngine.Random.Range(0,10);

This requires that you do indeed have 10 questions in the pool naturally! But if that is what you mean then this is the line you want to change.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I use LayerMask to check collision? 1 Answer

Trail of color behind the player 2D c# 1 Answer

[Quiz game] How do I make players contribute with questions? 1 Answer

Quiz Game Random Question Stuck 1 Answer

Immortal enemies when reanimated, or pooled game objects re-activated do not respond to collisions? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges