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 audibles · Oct 29, 2013 at 06:56 AM · gameobjectlistremove

Remove all duplicates from a list

Feeling dumb, but I couldn't find an answer here or the forums.

I have a list of gameobjects and I want to remove all occurrences of a specific gameobject from that list. I've played around using RemoveAll() and ForEach loops, but no luck so far.

To illustrate my request:

 List<GameObject> testList = new List<GameObject>();
 testList.Add (obj1);
 testList.Add (obj1);
 testList.Add (obj2);
 testList.Add (obj3);

What I would like to be able to do, for example, is to remove all occurrences of obj1 from testList. The testList should then only return obj2 and obj3.

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 Tarlius · Oct 30, 2013 at 05:24 AM 0
Share

While the Linq options are definitely the cleanest, its worth bearing in $$anonymous$$d that if you are building an iOS game some of the functions can break at runtime. I'm not sure which ones cause problems, but if you have trouble do check it out!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Huacanacha · Oct 30, 2013 at 05:02 AM

You should be able to use RemoveAll() like this:

 list.RemoveAll(obj => obj == objToDelete);

It works for me in Unity... here's my sample console output:

 >  List<String> list = new List<String> {"a","b","b","c"};
 >  String.Join(",", list.ToArray());
 a,b,b,c
 >  list.RemoveAll(obj => obj == "b");
 2
 >  String.Join(",", list.ToArray());
 a,c

Edit: updated to the corrected answer from the comments below.

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 audibles · Oct 30, 2013 at 05:43 AM 0
Share

That actually may not work. I'm trying to remove all occurrences of one object, not all duplicates of all objects. Unless I misunderstood how Distinct() works.

To elaborate:

If testList contains (obj1, obj1, obj2, obj2, obj3), I want to be able to remove all obj1 and leave me with (obj2, obj2, obj3). Is this possible?

Also, I am making an iOS project.

avatar image Huacanacha · Oct 30, 2013 at 06:34 AM 2
Share

$$anonymous$$y apologies... I must have read the other answer more closely than your question!

You should be able to use RemoveAll() like this:

 list.RemoveAll(obj => obj == objToDelete);

It works for me in Unity... here's my sample console output:

 >  List<String> list = new List<String> {"a","b","b","c"};
 >  String.Join(",", list.ToArray());
 a,b,b,c
 >  list.RemoveAll(obj => obj == "b");
 2
 >  String.Join(",", list.ToArray());
 a,c
avatar image audibles · Nov 04, 2013 at 05:37 PM 0
Share

That's what I was looking for! You should convert your comment to an answer so I can accept it. Thanks a lot!

avatar image Huacanacha · Nov 04, 2013 at 05:47 PM 0
Share

Done... original answer has been updated!

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

17 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

Related Questions

Get GameObject from List based on attached script or assigned name 3 Answers

Find specific prefab in a list 2 Answers

In a tile-based game, best way to select multiple tiles via dragging? 1 Answer

A node in a childnode? 1 Answer

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