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 riskaanisah · Dec 05, 2018 at 12:53 AM · randomrandom.range

random range can stop when the index matches the input and without duplicate question display

hi i need help with random range. in this code i have an array to store questions. i want to make it appear random when the game start. questionIndex = Random.Range(0, questionPool.Length); this managed to randomly but there is still duplication and does not stop right according to the index. please help me

 public SimpleObjectPool answerButtonObjectPool;
     public Text questionText;
     public Text scoreDisplay;
     public Text scoreDisplay2;
     public Text scoreDisplay3;
     public Text timeRemainingDisplay;
     public Transform answerButtonParent;
 
     public GameObject questionDisplay;
     public GameObject roundEndDisplay;
     public GameObject GameOverDisplay;
     public GameObject UIDisplay;
     public Text highScoreDisplay;
 
     private DataController dataController;
     private RoundData currentRoundData;
     private QuestionData[] questionPool;
 
     private bool isRoundActive = false;
     private float timeRemaining;
     private int playerScore;
     private int questionIndex;
     private List<GameObject> answerButtonGameObjects = new List<GameObject>();
 
     void Start()
     {
         audioPlayer = gameObject.AddComponent<AudioSource>();
         dataController = FindObjectOfType<DataController>();
         
         currentRoundData = dataController.GetCurrentRoundData();
         questionPool = currentRoundData.questions;
         
         timeRemaining = currentRoundData.timeLimitInSeconds;
         UpdateTimeRemainingDisplay();
         Time.timeScale = 1f;
         playerScore = 0;
         questionIndex = 0;
 
         ShowQuestion();
         isRoundActive = true;
     }
 
     void Update()
     {
         if (isRoundActive)
         {
             timeRemaining -= Time.deltaTime;
             UpdateTimeRemainingDisplay();
             
             if (timeRemaining <= 0f)
             {
                 GameOverRound();
             }
         }
     }
 
     void ShowQuestion()
     {
         RemoveAnswerButtons();
 
         questionIndex = Random.Range(0, questionPool.Length);
         QuestionData questionData = questionPool[questionIndex];
         questionText.text = questionData.questionText;
         
         for (int i = 0; i < questionData.answers.Length; i++)
         {
             GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
             answerButtonGameObjects.Add(answerButtonGameObject);
             answerButtonGameObject.transform.SetParent(answerButtonParent);
             answerButtonGameObject.transform.localScale = Vector3.one;
 
             AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
             answerButton.SetUp(questionData.answers[i]);
         }
     }
 
     void RemoveAnswerButtons()
     {
         while (answerButtonGameObjects.Count > 0)
         {
             answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
             answerButtonGameObjects.RemoveAt(0);
         }
     }

Comment
Add comment · Show 5
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 Vega4Life · Dec 05, 2018 at 12:59 AM 0
Share

Is it possible your QuestionData has a duplicate question in it? It's worth a look! I would start by looking in the RoundData - maybe a dupe got in there.

avatar image riskaanisah Vega4Life · Dec 05, 2018 at 01:29 AM 0
Share

alt text

datacontroller.jpg (63.2 kB)
avatar image riskaanisah Vega4Life · Dec 05, 2018 at 01:34 AM 0
Share

do i need to send my project? I have a problem when the match starts, he does display questions randomly. but when I proceed to the next question, sometimes it stops at 2 questions only, and 8 other questions have not been resolved. or questions continue to appear more than 10 questions. and there are questions that appear repeatedly and the same.

sorry if my english so bad

avatar image Vega4Life riskaanisah · Dec 05, 2018 at 01:46 AM 0
Share

That's up to you. If you bundle up your project, I can look at it. But I need to know exactly what it's supposed to do. When the game starts, it shows 1 question and continues for 9 more questions? 10 questions total? One question at a time?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vega4Life · Dec 05, 2018 at 04:05 PM

First the issue:


 void ShowQuestion()
     {
         RemoveAnswerButtons();
 
         questionIndex = Random.Range(0, questionPool.Length);


Here we get a random question from our pool. First thing I checked was to see if the questions were being removed from the array after they had been picked. They aren't, thus the dupe situation.


         if (questionPool.Length > questionIndex + 1)
         {
             questionIndex++;
             ShowQuestion();
         }


Here I noticed we are incrementing questionIndex, but quickly realized it didn't do anything because it gets overwritten when we get a new question. Thus why the game never ends sometimes. In fact, the game will only end (and keep giving you a question) when the index is one less the length of the question pool. Because (question.Length > questionIndex + 1). This is only false with 10 questions, when the questionIndex is randomly chosen to be 9.


Now a quick solution I tested and works fine (you can change it or fix it however you feel):


 private List<QuestionData> questionPool;
 
     void Start()
     {
         currentRoundData = dataController.GetCurrentRoundData();
         questionPool = new List<QuestionData>(currentRoundData.questions);


I made questionPool a list. This allows us to remove the questions that are picked and easily know how many questions are left.


 void ShowQuestion()
 {
     RemoveAnswerButtons();

     questionIndex = Random.Range(0, questionPool.Count);
     QuestionData questionData = questionPool[questionIndex];

     // Remove question from list so we don't get it again
     questionPool.Remove(questionData);



Change length to .count, and add a new line to remove the question we picked from the list.


         // If last question was correct && we have questions, continue
         if (isCorrect && questionPool.Count > 0)
         {
             ShowQuestion();
         }
         else
         {
             EndRound();
         }


Make sure the last question was correct before continuing, and just check if the list has things in it.


I tested this code and hit replay a few times and it works fine. Up to you if you want to refactor it, but it gets the point across. Nevertheless, game looks great, keep it up!


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
avatar image
0

Answer by riskaanisah · Dec 05, 2018 at 02:54 AM

link text


script-quiz.zip (5.3 kB)
Comment
Add comment · Show 20 · 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
avatar image Vega4Life · Dec 05, 2018 at 04:28 AM 0
Share

It would be better if you exported the entire project - just having code will make it difficult to understand where things go, etc. Just zip the entire unity project if you can.

avatar image riskaanisah Vega4Life · Dec 05, 2018 at 06:12 AM 0
Share

okay wait the project I made with English and Indonesian so you are not confused. the problem I mean I mean you can open it in the Folder Scene/$$anonymous$$uis/ Scene $$anonymous$$uis there are Persistent, $$anonymous$$enu, and Game scenes

link text

avatar image Vega4Life riskaanisah · Dec 05, 2018 at 03:41 PM 0
Share

Just wanted to let you know, I am looking at this and I do see the issue. :) Let me get an answer together for you soon.

Show more comments

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

102 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 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

Randomising list of integers and removing them from list once the task assigned to the integer in the project is completed 0 Answers

Random.range multiple instantiations without repetition 1 Answer

reload random scene 2 Answers

Randomly setting a boolean to true? 3 Answers

Semi-Random Or Engine 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