Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SomeoneNamedTom · Apr 13, 2021 at 06:12 AM · listproceduralfor-loopremove

RemoveAt() not working within for() loop.

I am spawning trees in random locations around my landscape mesh. My code essentially locates all vertices on the landscape mesh, keeps only the vertices that would be good tree locations (based on criteria I've set), and saves these vertices in a list called "treeSpawnLocations". It then determines how many trees to spawn, based on a predetermined "treeDensity" float, and it uses a for() loop to instantiate each tree at a valid tree location (a random "treeType" from an array of tree prefabs, and at a random y rotation).

Here's where my problem is. I don't want multiple trees spawning at the same location. So after each tree spawns, within the for() loop, I use treeSpawnLocations.RemoveAt(t) to remove that spawn point from the list of possible spawn locations. The code works perfectly except that multiple trees still spawn at the same locations. The code seems to completely ignore that line. Any idea why?

TLDR: I'm trying to remove an item from a list in each for() loop so that item doesn't get pulled again in the next loop, but it doesn't get removed.

Here's the section of code relevant to my question:

 int treesToSpawn = Mathf.RoundToInt(treeSpawnLocations.Count * treeDensity);
 for (int i = 0; i < treesToSpawn; i++)
 {
     int t = Random.Range(0, treeSpawnLocations.Count - 1);
     Instantiate(treeTypes[Random.Range(0, treeTypes.Length - 1)], treeSpawnLocations[t], Quaternion.Euler(Quaternion.identity.x, Random.Range(0, 360), Quaternion.identity.z));
     treeSpawnLocations.RemoveAt(t);
 }
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 andrew-lukasik · Apr 13, 2021 at 06:31 AM

RemoveAt works fine. Your treeSpawnLocations contains duplicate entries.

 void RemoveDuplicates ( List<Vector3> list )
 {
     var set = new HashSet<Vector3>( list );
     list.Clear();
     list.AddRange( set );
 }
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 SomeoneNamedTom · Apr 13, 2021 at 06:11 PM 0
Share

Wow, you're right! I looked back at my code and realized that yep, there are some instances when a vertex could be listed twice. I guess I overlooked that. Now I just have to figure out the most performant way to either prevent duplicates from occurring or to remove all duplicates at the same time after instantiating the tree. I could use another for() loop to remove all duplicates, but that would add another ~800,000 loops at the start of my game... I'll have to get creative. Thanks for your help.

avatar image andrew-lukasik SomeoneNamedTom · Apr 13, 2021 at 06:43 PM 0
Share

HashSet is a reasonable option here because it's Add/Push is O(1) operation and not O(n) (repeated iteration over long collection is a big no-no, performance wise). I suggest you use HashSet<Vector3> when assembling that list and convert it to List<Vector3> when it's done.

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

124 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

Related Questions

List That Won't Remove The Last Two Elements 3 Answers

A node in a childnode? 1 Answer

build settings screen - how to delete old/unused scenes ? 2 Answers

Remove and Destroy Object from Linked List C# 2 Answers

How to remove objects from a list ? 3 Answers


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