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 IsaiahKelly · Mar 13, 2017 at 09:54 PM · comparecomparisonmatchingequal

Check value equality of two Instances of the same type

Example: I have two game objects (object A and object B) in the scene with a light component attached to each. I want to compare the light components on both objects to see if they have the same exact values (Range, Color, Intensity, etc.).

Essentially I want something like if(LightComponentA == LightComponentB) to only return true if all field or property values on both instances match. Is there any simple way to do this, or do I just need to use reflections and iterate over each field of the object instance and then compare with my own function?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jjcrawley · Mar 17, 2017 at 01:52 AM

You'll probably have to right your own equals method. Something along these lines should do the trick:

 public static bool SameLight(Light firstLight, Light secondLight)
 {       
      if(firstLight.color != secondLight.color)
      {
           return false;
      }
      else if(firstLight.type != secondLight.type)
      {
           return false;
      }
      //for all other variables you want to check
      
      return true;
 }

This is written as a static method as you can't inherit from the light component, as a consequence, you can't override any of their default operators. Unity does there own background checks to determine whether their backend representation is the same, which is basically what occurs each time you use the == operator or the Equals method, for Unity objects. What classifies as a valid Equals case is up to, if your valid case is that they have all the same values, then you need to check through each value to determine whether they are the same. No shortcuts I'm afraid. You won't have to use reflection either, as the fields that you're concerned about are publicly accessible. Won't have to use iteration either. Just check the values that you care about. If that's all of them, then check all of them.

Hopefully this helps.

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 IsaiahKelly · Mar 18, 2017 at 02:43 AM 0
Share

Thanks for the detailed explanation! I suspected that I'd probably need to something like this, but your example is still very useful in helping me get started.

avatar image
1

Answer by IgorAherne · Mar 13, 2017 at 11:08 PM

Yes, specify your own function in the class

for example:

 public bool Equals(YourClass other){ 
 //returns true only if both bools in classes are equal AND both myObj point to the same thing.
        return this.myBool == other.myBool && ReferenceEquals(this.myObj, other.myObj);
 }

To make it even shorter, you can override the == thing, which is just a function, behind the curtains.

 public static bool operator ==(YourClass left, YourClass right) {
         // If both are null, or both are same instance, return true.
         if (System.Object.ReferenceEquals(left, right)) {
             return true;
         }
 
         // If one is null, but not both, return false. 
         if (((object)left == null) || ((object)right == null)) {
             return false;
         }
 
         return left.Equals(right);
     }

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 IsaiahKelly · Mar 17, 2017 at 01:17 AM -1
Share

I'm afraid I didn't example my objective here correctly. If I understand your examples correctly they are just comparing a single custom field or the instances themselves. I want to compare all fields between two instances of the same kind. I've rewritten by question to hopefully explain my goal better.

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

65 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

Related Questions

bool to true from a list comparison. 1 Answer

Unity see different objects as the same 0 Answers

How to campare gameObject.name with string when gameObject have additional numbers or words? 2 Answers

Compare if object is assigned specific Material 1 Answer

Check if Material of a gameObject == a material variable (instance)? 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