- Home /
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?
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.
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.
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);
}
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
Follow this Question
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