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 /
  • Help Room /
avatar image
0
Question by Glurth · Mar 20, 2016 at 11:38 PM · vector3listcontains

How do I properly use List-Vector3-.Contains()?

for some reason I seem to be getting different results with these two different code snippets. In both cases I'm searching a list of Vector3's for a particular Vector3 VALUE.

In all the code below tempVertexList is defined as :

  List<Vector3> tempVertexList; 

This version uses List.Contains() (Which I though might be faster than "by hand")

   //this version seem to fail to find matches
       if(tempVertexList.Contains(meshVertices[i]))
                         found=true;

And, this version is "by hand", which seems to find more matches:

 //this version seem to find all matches properly
     foreach (Vector3 listVector in tempVertexList)
     {
        if (listVector == meshVertices[i])
           { found = true;  break; }
     }

Why do these give different results? Does "Contains" use a different comparison operator? How can I fix that?

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
3
Best Answer

Answer by RudyTheDev · Mar 21, 2016 at 12:00 AM

That's because .Contains() ends up using Vector3.Equals() (via EqualityComparer<T>.Default since Vector3 doesn't implement IEquatable), while manual comparison uses ==. Equals() literally compares the x/y/z, while == compares magnitudes.

Here's (disassembled) Unity code for Vector3:

 public override bool Equals(object other)
 {
   if (!(other is Vector3))
     return false;
   Vector3 vector3 = (Vector3) other;
   if (this.x.Equals(vector3.x) && this.y.Equals(vector3.y))
     return this.z.Equals(vector3.z);
   return false;
 }

 public static bool operator ==(Vector3 lhs, Vector3 rhs)
 {
   return (double) Vector3.SqrMagnitude(lhs - rhs) < 0.0 / 1.0;
 }

You would need to implement your own EqualityComparer and pass it to the .NET collections during construction if you want to use things that do equality checks.

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 Glurth · Mar 21, 2016 at 12:48 AM 0
Share

"You would need to implement your own EqualityComparer and pass it to the .NET collections during construction if you want to use things that do equality checks." I was afraid it was something like that. Studied for a while how to specify a comparer myself, but kept getting stuck on the fact that Vector3 is sealed. I think the fact that I need to pass it to the container's constructor is what I was missing, I'll try my searches again with that i $$anonymous$$d. $$anonymous$$any thanks for the help!

avatar image RudyTheDev Glurth · Mar 21, 2016 at 06:38 PM 1
Share

The comparer itself is just:

 public class Vector3Comparer : IEqualityComparer<Vector3>
 {
     public static readonly Vector3Comparer Default = new Vector3Comparer();
     public bool Equals(Vector3 a, Vector3 b) { return a == b; }
     public int GetHashCode(Vector3 a) { return a.GetHashCode(); }
 }

and then just Vector3Comparer.Default. But yeah, the List<>s don't use custom ones, so you'd need to edit Vector3, which you can't. Works only for stuff like Dictionary.

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

42 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

Related Questions

List.Contains not working as intended 0 Answers

Find child object that Contains string from list 0 Answers

list.contains problem 1 Answer

Unique list of Vector3's 1 Answer

Sort a list of class by a vector variable in the class 2 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