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 14, 2014 at 02:55 AM · c#listrandomindexorder

How can I display 4 items in random order but never double?

So I am displaying 4 GUI Buttons.. Right now I am using a forloop to display them. The only issue is its displaying them the exact same oder everytime. Which i know thats because i havent written otherwise but thats where I need the help. I don't want doubles of an button, just to have them displayed in a random order. Any ideas??

  //Display the answers.
                  for (int i = 0; i <= 3; i++)
                  {
                      if (GUI.Button(new Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2) - (25 / 2) + i * 30, buttonWidth, buttonHeight), mathList[mathIndex].Answers[i]))
                      {
                          //If correct answer...
                          if (i == mathList[mathIndex].CorrectAnswer)
                          {
                              Debug.Log("Correct");
  
                              if (mathList.Count >= 1)
                              {
                                  Debug.Log("Deleted question" + mathIndex.ToString());
                                  mathList.Remove(mathList[mathIndex]);
                                  GetMathQuestion();
                              }    //Here we would add the Ending...
                              else { Debug.LogError("Out Of Questions To Display"); }
                          }
                          else //if incorrect....
                          {
                              Debug.Log("Incorrect");
                              GetMathQuestion();
                          }
                      }
                  }
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

2 Replies

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

Answer by fafase · Dec 14, 2014 at 07:35 PM

Have them in a list and when you draw one remove it from the list. Repeat until the list is empty:

 Button[]array;
   
 Button DrawRandomButtonOrder(){
     List<Button>list = new List<Button>(array);
     while(list.Count > 0) // while the list is not empty
     {
        int rand = Random.Range(0, list.Count); // Get a random value
        if(GUI.Button(list[rand])){} // This line needs more details
        list.RemoveAt(rand); // remove it
     }
 }

I

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 15, 2014 at 10:31 AM 0
Share

The "Button" type is not recognized...

avatar image fafase · Dec 15, 2014 at 06:51 PM 0
Share

You need to create it and fill it with the needed info. This is just the algorithm.

avatar image
0

Answer by mattyman174 · Dec 14, 2014 at 03:11 AM

Make a list of current chosen buttons and when you go to choose a new random button, check the list to see if the new random button already exists, if it does, then choose another random button instead.

Comment
Add comment · Show 3 · 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 fafase · Dec 14, 2014 at 07:30 PM 0
Share

That is not the best approach. What if the list is 1 item long? Endless loop, what if it is 2 items long? 50% chances of endless loop. This is not suitable unfortunately since when there is a chance, it will happen.

avatar image mattyman174 · Dec 14, 2014 at 11:05 PM 0
Share

Sorry, i thought it was painfully obvious to avoid bad program$$anonymous$$g practices such as that so i didnt include it in my suggestion.

avatar image glady · Aug 10, 2016 at 06:51 PM 0
Share

hi @fafase @mattyman174 on what button could i input this code ? do i need another scene? please help me!

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

27 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

Related Questions

Pick between two floats 2 Answers

Selecting random indexes without repetition 3 Answers

Printing a GUI selection grid in order 1 Answer

Problems with removing a random from a list 2 Answers

Printing an ordered list 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