Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JoshMBeyer · Dec 18, 2014 at 01:00 AM · listrandomforloop

Randomize Text on 4.6 UI Button's

So there are 4 button's in the scene. Answer 0 Button - Answer 3 Button, at runtime, their text changes to the text from a class called Question(String Answers, String QuestionText, int CorrectAnswerIndex); Then I have a list of these questions. I am almost done with my project I just need to make it to where the answers are randomized and my work will be complete. Here is the code that handles filling in the button's text. If you need more information I will post more of the script. I am just stuck and can't quite figure this out.

 //------------------------------------------------------
     void displayQuestion (Question question) {
     //------------------------------------------------------
         questionText.text = question.QuestionText;
 
         // Variable answers, as well as answerCount,
         // introduced for faster access fo some data.
         List<string> 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.
         
 
         for (i = 0; i < answerCount; ++i)
         {
             answerButton [i].SetActive (true);
             answerButtonText [i].text = answers [i];
         }
 
         // Then we should hide extra buttons, which is
         // not containig answers.
         for (i = answerCount; i < maxNumberOfAnswers; ++i)
         {
             answerButton [i].SetActive (false);
         }
     } // ~displayQuestion
 
Comment
Add comment
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

1 Reply

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

Answer by HYPERSAVV · Dec 18, 2014 at 01:44 AM

Randomizing your selection should do the trick. With each selection we remove it from the List, and the next iteration we can choose from answerCount - i.

 for (i = 0; i < answerCount; ++i)
 {
    answerButton [i].SetActive (true);
    int selectedAnswer = Random.Range(0, answerCount - 1 - i);
    answerButtonText [i].text = answers [selectedAnswer];
    answers.RemoveAt(selectedAnswer);
 }

EDIT: Corrected RemoteAt to RemoveAt

Comment
Add comment · Show 2 · 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 JoshMBeyer · Dec 19, 2014 at 04:21 AM 0
Share

This isn't working actually. I just found a few bugs that are cause by this.

well as i said the issue is with the randomization of the answers. What is happening is that at runtime, a question and the 4 answers are pulled from the list. Question[index] and Answer[0] Answer[1] Answer[2] and Answer[3] Then, they are displayed randomly as text to the buttons. So, the index is involuntarily being switched around. So Answer[1] could be in the answer[3]'s slot. So if the correct answer is Answer[1], i would have to click Answer[3].

avatar image HYPERSAVV · Dec 19, 2014 at 08:04 PM 0
Share

To be fair, I have no knowledge of your selection logic so I was going off of what was asked (displaying text randomly) :) Although, RemoteAt should be RemoveAt().

Try to think of this problem a little differently. You're concerned with randomizing the text across multiple buttons, but when we do this we cannot say that Button N maps to Answer[N] anymore. When we set a button with an answer, we need the button to know what index it maps to in the original Answers array.

...or we can just shuffle the Buttons around! Button N will map to Answer[N] now. You will just need the logic to dynamically move the buttons around, perhaps by creating Empty objects in your scene at the location of each possible button, and using those Transforms to shuffle the buttons around.

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

28 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

Related Questions

A node in a childnode? 1 Answer

Random instantiation endlessly? 1 Answer

How do I get a list to randomly select.... 1 Answer

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer

Random role assigning from string array 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