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 ma22be61 · Jan 17, 2018 at 08:52 PM · arraylistrandomrandomizetoarray

Randomly remove list values

I got a platform with 32 obstacles which are all included into an array (by tag). I transfer all of these values to a list using .ToList.

Everything works fine and all of the values are transferred to the list. Once the scene is played all of the 32 platforms are spawned which is also good.

What I want to do is to randomly remove values from the list and then transfer them back to the array (I know how to transfer them but not how to randomly remove the values). I tried with RemoveRange but this only removes values from a starting value to an end value even if randomized with Random.Range.

I want to randomly remove multiple items from the list not in order (for example remove the first item on the list then the 3rd then the 5th etc...).

I could do that by writing list.Remove(list[Random.Range(1,32)]); but I hope there are better methods..

Below is my code :

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class RandomizeSpawn : MonoBehaviour {
     public GameObject wallObs;
     private GameObject[] allspawnPoint;
     private GameObject[] spawnPoint;
     private List<GameObject> disabledSpawns = new List<GameObject>();
 
     private bool callOnce;
 
     void Start () {
         wallObs = GameObject.Find ("Wall");
         callOnce = true;
     }
         
     void SpawnObstable () //Where the obstacles are spawned
     {
         foreach (GameObject respawnPoint in spawnPoint)
         {
             Instantiate(wallObs, new Vector3 (respawnPoint.transform.position.x,wallObs.transform.position.y,respawnPoint.transform.position.z), respawnPoint.transform.rotation);
             callOnce = false;
         }
 
     }
 
     void Update () {
         allspawnPoint = GameObject.FindGameObjectsWithTag ("Spawner");
         if (callOnce == true) {
             disabledSpawns = allspawnPoint.ToList ();
             Debug.Log (disabledSpawns.Count);
             RandomizeSpawns ();
 
         }
     }
 
     void RandomizeSpawns () //Where I remove only 1 item using random.range 
     {
         disabledSpawns.Remove(disabledSpawns[Random.Range(1,32)]);
         spawnPoint = disabledSpawns.ToArray (); //this is where I transfer the objects from the list to the array to get spawned
 
         SpawnObstable ();
 
     }
 }
 


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 raoul_95 · Jan 18, 2018 at 06:12 AM 0
Share

Convert the list to array and use random function with array length-1 as limit

1 Reply

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

Answer by Kishotta · Jan 18, 2018 at 08:24 AM

You could do:

 disabledSpawns.RemoveAt(Random.Range(0, disabledSpawns.Count - 1));
Comment
Add comment · Show 4 · 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 ma22be61 · Jan 18, 2018 at 05:58 PM 0
Share

Only removes 1 object. I want multiple objects (for example 10) to be removed from the list (which is 32 objects long) at once

avatar image davidrochin ma22be61 · Jan 18, 2018 at 07:57 PM 2
Share

Well, you can just use a for loop then: for(int x = 0; x < 10; x++){ [Code to remove random spawnpoint here] }

avatar image ma22be61 davidrochin · Jan 19, 2018 at 10:03 AM 0
Share

Thanks to all of you , this works

 for (int x = 0; x < 12; x++) {
             disabledSpawns.RemoveAt(Random.Range(0, disabledSpawns.Count - 1));
             Debug.Log (disabledSpawns.Count);
         }
avatar image Harinezumi · Jan 19, 2018 at 10:44 AM 0
Share

Watch out, it should use Random.Range(0, disabledSpawns.Count), because Random.Range(int $$anonymous$$, int max)is inclusive-exclusive (the float version is inclusive-inclusive). If you leave it at disabledSpawns.Count - 1it will never try to remove the last element!

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

93 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

Related Questions

Pick between two floats 2 Answers

A node in a childnode? 1 Answer

Random select from array and spawn 1 Answer

Selection list from Array Unity - Random - GameObjects array 1 Answer

Creating a record of values generated with Random.Range 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