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 Bigproblem01 · Aug 18, 2014 at 02:07 PM · listrandomonce

Pick a memeber form the list only once

Hi Guys,

I have a List and what I am trying to do is select a random string from that list only once. When the list runs out of strings, refill it and do the procedure again. Any hints on how I can do this? I know the List has .Add and .Remove operations for example but I can't achieve what I want using only those.

Thanks

Comment
Add comment · Show 1
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 OrbitGames · Aug 18, 2014 at 02:13 PM 0
Share

use List.RemoveAt(int index) where the index is a random number between including zero and the list length -1

3 Replies

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

Answer by Scribe · Aug 18, 2014 at 02:15 PM

randomly picking an object from a list only once is the exact same as a 'shuffle'

There are lots of easy algorithms on how to shuffle a list, a common one to use is the Fisher-Yates shuffle which can be implemented as:

 for(int i = 0; i < list.Length; i++){
     int j = Random.Range(i, list.Length);
     String t = list[i];
     list[i] = list[j];
     list[j] = t;
 }

Once shuffled you can then read from the list in order and will only ever get each entry once:

 for(int k = 0; k < list.Length; k++){
     Debug.Log(list[k]);
 }

Scribe

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 HarshadK · Aug 18, 2014 at 02:24 PM 1
Share

This one is better than my approach. Takes away a hell lot of headache.

avatar image Bigproblem01 · Aug 19, 2014 at 10:57 AM 0
Share

Thanks a lot Scribe, solved my problem!

Just a correction for other users that may have a similar case: In Lists, when you want to access the number of members you use "Count" while in Arrays it's "Length".

avatar image Scribe · Aug 19, 2014 at 11:59 AM 0
Share

No problem, and thanks for the correction, I normally end up using arrays for everything :P

avatar image
0

Answer by HarshadK · Aug 18, 2014 at 02:11 PM

The logic would be something like:

  1. Have two set of lists - one to store all the possible values and one to select values from.

  2. Add all items from your list of all possible values to your list to select values from.

  3. Choose value from the list to select values.

  4. Remove that chosen items from the list to select values.

  5. Once your list to select values is empty add all items from your list of all possible values into your list to select values.

  6. Repeat the procedure from #3 to #6 again and again.

Since you know the number of items you can add to your list to select values it would be efficient to have an array rather than list for that.

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 Bigproblem01 · Aug 19, 2014 at 10:58 AM 0
Share

This seems one way to go, although Scribe's answer is more efficient. Thanks a lot for your input nonetheless! : )

avatar image
0

Answer by kyzzer · Jun 03, 2021 at 11:53 AM

I know this is late, but for anyone else stuck you could use a separate Hashset along with your List and check if the gameobject is contained in the Hashset. If not then just -- your for loop. So for example something like this:

 public List<Transform> list=new List<Transform>();
     private HashSet<Vector3> track = new HashSet<Vector3>();
     
     public GameObject orb;
 
     private void Start()
     {
         
         
         for(var i = 0; i < 14; i++)
         {
             Vector3 spawnPts = list[Random.Range(0, list.Count)].position;
             if (!track.Contains(spawnPts))
             {
                 track.Add(spawnPts);
                 Instantiate(orb, spawnPts, orb.transform.rotation);
             }
             else {
                 i--;
             }
              
             
             
         }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to Detect Touch on a Random String from a List? 1 Answer

Spawn various enemies at random 1 Answer

How to prevent picking the same combination in array? 1 Answer

How do I Instantiate a prefab in a randomly generated location specific to tile type (2D Procedural game) 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