• 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 Basseyh · Aug 07, 2017 at 06:22 PM · color32

Operator '==' cannot be applied to operands of type 'Color32' and 'Color32'.

I've been trying to make a script that reads a texture and then spawns a tile on every pixel, and because of float imprecision I use Color32 so the RGB values go from 0 to 255 instead of 0 to 1. But now, when comparing colors to make sure they're the same, it gives me the following error: Operator '==' cannot be applied to operands of type 'Color32' and 'Color32'. Is it impossible to compare two color32's or am I doing something wrong?

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

5 Replies

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

Answer by Basseyh · Aug 08, 2017 at 07:15 PM

Okay, I just found a thing that worked, but I don't know if it is the correct way to do it. instead of

Color32 foo1 = Color.black;
Color32 foo2 = foo1;
Debug.log(foo1 == foo2);
I did
Color32 foo1 = Color.black;
Color32 foo2 = foo1;
Debug.Log(foo1.Equals(foo2));
this seems to work

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 a161803398874 · Aug 09, 2017 at 04:36 AM 0
Share

works fine

avatar image
2

Answer by tanoshimi · Aug 07, 2017 at 06:51 PM

What's wrong with:

 if(color1.r == color2.r && color1.g == color2.g && color1.b == color2.b) { ... }

?

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 Basseyh · Aug 08, 2017 at 07:08 PM 0
Share

Yeah, I just did that as a workaround, but it looks like a mess, which it is.

avatar image
-1

Answer by Bunny83 · Aug 07, 2017 at 10:32 PM

I doubt that this error refers to "UnityEngine.Color32". Such an error usually uses the full type name. So i guess you named one of your own types "Color32". If not you should give more information about the code that produces this error.

You can try printing the actual type name of your two operands like this:

 Debug,Log("Type: " + col.GetType().FullName);

Tell us what the console shows.

In general you can compare two instances of the same value type without any further work, It does automatically compare all fields of the struct. However that does only work if it's the same type.

edit

Actually i was wrong. C# doesn't seem to provide a default == operator for custom value types. The struct has to explicitly overload the ==operator. However Color32 does not implement one but Color does. Since Color32 can implicitly converted into Color the comparison will work when at least one operand is a Color instead of Color32. However since both operands are Color32 there's no == operator defined.

I would almost call that a bug (not really but a missing feature for sure). Most of the commonly used built-in value types in Unity specify a custom == operator (Vector2 / 3 / 4, Quaternion, Matrix4x4, Color, Bounds, Rect). However there are some which do not and unfortunately Color32 is one of them.

Even "Equals" can be used, it's not recommended as Equals is a method of the Object base class. That means the parameter need to be boxed into an object and create garbage. So the best solution is to manually compare each component. You can create an extension method to do this if you fear the long if statement

 public static class Color32Ext
 {
     public static bool IsEqualTo(this Color32 aCol, Color32 aRef)
     {
         return aCol.r == aRef.r && aCol.g == aRef.g && aCol.b == aRef.b && aCol.a == aRef.a;
     }
 }

This is much better than using Equals.

ps: Like with most missing features there's already a feature request, so feel free to upvote it.

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 Basseyh · Aug 08, 2017 at 07:02 PM 1
Share

Okay, so for the first type I get this debug log:

prefabPixelColor: UnityEngine.Color32
UnityEngine.Debug:Log(Object)
from this line:
Debug.Log("prefabPixelColor: " + prefab$$anonymous$$ap[prefabIndex].pixelColor.GetType().FullName);

and for the second one I got this debug log:

imageColor: UnityEngine.Color32
UnityEngine.Debug:Log(Object)
from this line:
Debug.Log("imageColor: " + pixels[pixelIndex].GetType().FullName);

At no point did I create a struct named Color32, and the variable types seem to be the same, so that couldn't be it.

Also, I just did this:

        Color32 foo1 = Color.black;
        Color32 foo2 = foo1;
        if(foo1 == foo2)
        {
            Debug.Log("Wait a second, this DOES work!?")
        }
and it threw me the same error.

avatar image Bunny83 Basseyh · Aug 09, 2017 at 02:22 AM 0
Share

I've updated my answer. It looks like Unity hasn't implemented a == operator for Color32s.

avatar image
1

Answer by Dave-Carlile · Aug 08, 2017 at 07:16 PM

You might try if (color1.Equals(color2))...

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 Bunny83 · Aug 09, 2017 at 02:25 AM 0
Share

This will work but create garbage as "color2" need to be boxed in order to be passed as "object" parameter. It's actually better to manually compare the components like tanoshimi suggested. Or if you fear the long if statement, implement an extension method like i mentioned in my answer. $$anonymous$$ake sure you also upvote the feature request to add an == operator to the Color32 struct

avatar image
0

Answer by N-8-D-e-v · Jul 06, 2021 at 12:17 AM

I just wrote this super quick and easy extension method that compares the 'r', 'g', 'b', and 'a' channels of two Color32 structs to check if they are equal.

     /// <summary>
     /// A set of extension methods for the <see cref="Color32"/> struct.
     /// </summary>
     public static class Color32Extensions
     {
         /// <summary>
         /// Returns true if the <see cref="Color32.r">red,</see> <see cref="Color32.g">green,</see>
         /// <see cref="Color32.b">blue,</see> and <see cref="Color32.a">alpha</see> channels of both
         /// <see cref="Color32">Color32s</see> passed in are equal.
         /// </summary>
         public static bool IsEqualTo(this Color32 first, Color32 second) => 
             first.r == second.r && first.g == second.g && first.b == second.b && first.a == second.a;
     }

Here's an example use-case

 var firstColor = new Color32(255, 255, 255, 255);
 var secondColor = new Color32(255, 255, 255, 255);
 var thirdColor = new Color32(0, 0, 0, 0);
 
 Debug.Log(firstColor.IsEqualTo(secondColor)); // prints true
 Debug.Log(firstColor.IsEqualTo(thirdColor); // prints false

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

71 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why do I get this error while trying to create a color? 2 Answers

Convert Texture2D to array of uint and back 2 Answers

How can I assign one color to one vertex? 1 Answer

Can not compute type of conditional expression as `UnityEngine.Color32' and `UnityEngine.Color' convert implicitly to each other 2 Answers

How to type Color32 format into XML? 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