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 Lineweaver · Mar 19, 2020 at 10:34 PM · listrandomrandom.rangerepeatelements

Trying to avoid repeating elements on list (random.range)

This is a bit of a followup to this post: https://answers.unity.com/questions/1709250/transformrotate-on-z-axis-moves-the-entire-object.html?childToView=1709334#comment-1709334

I have a circle of 4 colors, and the player gets one of these colors randomly when it collides with a certain object.

What I'm trying to do is to make sure that the player doesn't pick the same color twice in a row. I tried clearing the list of colors and then resetting it by adding a copy of the original list to the list, like so:

 public class Player : MonoBehaviour
 {
     public float jumpForce = 10f;
     public string currentColor;
 
     public List<Color> colors;
     public List<Color> copy;
 
     Rigidbody2D rb;
     SpriteRenderer sr;
 
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         sr = GetComponent<SpriteRenderer>();
 
         SetRandomColor();
     }
 
     void Update()
     {
         if(Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
         {
             rb.velocity = Vector2.up * jumpForce;
         }
     }
 
     private void OnTriggerEnter2D(Collider2D col)
     {
         if(col.tag == "ColorChanger")
         {
             SetRandomColor();
             Destroy(col.gameObject);
             return;
         }
 
         if(col.tag != currentColor || col.tag == "Border")
         {
             Debug.Log("GAME OVER!");
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
         }
     }
 
     void SetRandomColor()
     {
         int index = Random.Range(0, colors.Count);
 
         switch(index)
         {
             case 0:
                 currentColor = "Cyan";
                 sr.color = colors[0];
                 colors.RemoveAt(0);
                 break;
             case 1:
                 currentColor = "Yellow";
                 sr.color = colors[1];
                 colors.RemoveAt(1);
                 break;
             case 2:
                 currentColor = "Purple";
                 sr.color = colors[2];
                 colors.RemoveAt(2);
                 break;
             case 3:
                 currentColor = "Pink";
                 sr.color = colors[3];
                 colors.RemoveAt(3);
                 break;
         }
 
         ResetColorList();
     }
 
     void ResetColorList()
     {
         if(!colors.Any<Color>())
         {
             Debug.Log("List is empty");
             colors.AddRange(copy);
         }
     }
 }


This is what the player looks like: alt text

The player does change color, and everything else in the code works , but I don't know how to avoid the repeating numbers in the list. Any help would be appreciated.

editor.png (137.0 kB)
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
0

Answer by martygrof3708 · Mar 20, 2020 at 12:33 AM

You could use a third temporary list to randomize the colors, like so:

         List<Color> temp = colors;
 
         while ( temp.Count > 0 )
         {
             int randomNumber = UnityEngine.Random.Range( 0 , temp.Count );
             copy.Add( temp[ randomNumber ] );
             temp.RemoveAt( randomNumber );
         }

This way there is a very small chance you could still get the same exact order.

Comment
Add comment · Show 1 · 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 Lineweaver · Mar 20, 2020 at 09:15 AM 0
Share

Do I use this methid once after the switch statement, or in every single case?

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

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

Creating a record of values generated with Random.Range 1 Answer

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

A node in a childnode? 1 Answer

Selection list from Array Unity - Random - GameObjects 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