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 /
This question was closed Aug 16, 2016 at 02:02 PM by nexusgeniuz for the following reason:

На вопрос ответили. Правильный ответ был принят

avatar image
0
Question by nexusgeniuz · Aug 15, 2016 at 05:24 PM · c#scripting problemlistcustom class

How to remove an item from a list of custom variables

I have an intVector2 class

 public class intVector2
 {
     public int x;
     public int y;
 
     public intVector2(int _x, int _y)
     {
         x = _x;
         y = _y;
     }
 }

In other script, I have created a list of them

 List<intVector2> allowedPoss = new List<intVector2>();

How do I remove some items from it?

 allowedPoss.Remove(new intVector2(1, 1)); //Doesn't works

P.S. sorry for my English.

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

  • Sort: 
avatar image
1
Best Answer

Answer by NoseKills · Aug 15, 2016 at 07:16 PM

 allowedPoss.Remove(new intVector2(1, 1));

doesn't work because on this line you create a new intVector2() using its constructor. The vector you create on this line is not in the list so it can't be removed either. Perhaps you have added another vector that has the same x and y values to the list but it is not the same vector.

As the C# documentation explains the object you pass in as the parameter to Remove is compared to the items on the list using IEquatable.Equals() if your class implements that interface. That's the key. Implement that interface to your intVector class and the Remove method will know which vectors should be considered "the same"

 public class intVector2 : IEquatable<intVector>
  {
      public int x;
      public int y;
  
      public intVector2(int _x, int _y)
      {
          x = _x;
          y = _y;
      }
      public bool Equals(intVector2 other)
      {
          if (other == null)
              return false;
 
          return other.x == x && other.y == y;
      }
  }

Your other options are to use RemoveAt() to remove from a specific index or to hold a refere nice to the vector you need to remove and use when you call Remove.

Hope I didn't make too many typos. I'm on mobile.

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 nexusgeniuz · Aug 16, 2016 at 01:29 PM 0
Share

Thank you very much!

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Find all objects with Tag and List their Transforms? 4 Answers

Update list on mouse click 1 Answer

List.Add (new CustomClass()) results in empty entry 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