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
1
Question by TvrtleWaterBear · Aug 20, 2015 at 07:52 PM · c#classlistsoverrideequal

Using the object.Equals() method for a custom class

I've created my own class Contact to hold a bunch of variables on a series of gameobjects, and a series of the Contacts are being held in a list. I'd ideally like to be able to use the built in methods like list.Contains() for the sake of convenience. From what I've been able to find on the .Contains() method, it uses a method named Equals() in the class which has to be overridden.

I'm attempting to return true from .Equals() when the string "id" is the same for the compared Contacts. What I have so far is as follows:

 public class Contact : object
 {
     public GameObject game_obj;
     public string id;
     public float level;
     public float health;
 
     public float distance = 0;
     public Vector3 direction = new Vector3(0, 0, 0);
 
     public bool more_stuff = false;
 
     //constructors
     public Contact(GameObject go)
     {
         game_obj = go;
         id = go.name;
         level = go.GetComponent<NpcManager>().level;
         health = go.GetComponent<NpcManager>().health;
     }
 
     //comparator
     public override bool Equals(Contact other)
     {
         Debug.Log("Called it.");
         //check for null vals and compare types
         if (other == null || GetType() != other.GetType())
         {
             return false;
         }
         return (id == other.id);
     }
 
     public override int GetHashCode()
     {
         return base.GetHashCode();
     }
 
 }

The problem I'm having is that when I try to override the Equals method, unity throws an error saying that Contact.Equals(Contact) cannot override because object.Equals(object) is not a method.

The articles I've used: object.equals() and list.contains()

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 cjdev · Aug 20, 2015 at 07:56 PM

Check out this article. The problem is you need to override the System.Object Equals method and then implement your own Equals method for your object like this:

 public override bool Equals(System.Object obj)
 {
     if (obj == null)
         return false;

     Contact c = obj as Contact ;
     if ((System.Object)c == null)
         return false;

     return id == c.id;
 }

 public bool Equals(Contact c)
 {
     if ((object)c == null)
         return false;

     return c == c.id;
 }


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 TvrtleWaterBear · Aug 20, 2015 at 10:14 PM 0
Share

Thank you! That article was also very helpful.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is Start() from my base class ignored in my sub-class? 4 Answers

Listing players by score 1 Answer

Getting NullReferenceException when creating class instance using List 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