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 Crumpet · Jul 08, 2018 at 09:07 AM · tilemaplistsnulltilesclear

TileBase array conversion to list not clearing?

Hi, I don't post here often so sorry if my format is a bit messed up. I am using System.Linq to change the array over to a list and then attempting to clear everything that is null inside the list. I've only just started using tilemaps and have noticed that a lot of tilemaps in my array are null. so I put them into a list to clear them out. This for some reason only clears out half of the list. Here is my code.

         BoundsInt bounds = myNodesTilemap.cellBounds;
         TileBase[] allTiles = myNodesTilemap.GetTilesBlock(bounds);
         
         tempTileList = allTiles.ToList<TileBase>();
 
         for (int i = 0; i < tempTileList.Count; i++)
         {
             if (tempTileList[i] == null)
             {
                 tempTileList.RemoveAt(i);//this only removes half of the list, if I don't run this line of code I get a list of 48. If I run it I get a list of 24.
             }
             if (tempTileList[i] != null)
             {
                 Debug.Log(tempTileList[i].name);//this never debugs anything.
             }
         }
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
Best Answer

Answer by ShadyProductions · Jul 08, 2018 at 09:11 AM

 allTiles.Where(f => f != null).ToList();

Should be enough using LINQ.

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 Crumpet · Jul 08, 2018 at 09:16 AM 0
Share

This fixed it, I don't understand why though. Any idea?

avatar image ShadyProductions Crumpet · Jul 09, 2018 at 06:59 AM 0
Share

Iterating over a list / array and removing indexes has weird side effects, This because say you are looping over a list of 10 objects, you remove one, and you have 9 left while you are looping over 10 objects which can cause index out of range issues and its not even permitted in foreach loops, on for loops it re evaluates the initial loop accessing the list's .Count as you are using which is why it does not throw an index out of range exception but still gives you weird results. It would be better to ins$$anonymous$$d loop over a list and add all values that are not null to a new list and return that. But this is the easier way since you're already intending to use .ToList from LINQ. $$anonymous$$ight aswel use its where predicate.

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

89 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

Related Questions

Best way to make a big 2D map as smooth as possible? 1 Answer

Tile palette and grid selection 0 Answers

Getting the Collider of a Tile 0 Answers

How to get component from a tiled prefab which belongs to a tilemap 1 Answer

How to count Tiles in a sourinding Area around an Object? 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