Search Unity

trying to compare 2 material colors

Discussion in 'Scripting' started by brunoenvia, Oct 16, 2019.

  1. brunoenvia

    brunoenvia

    Joined:
    Aug 5, 2019
    Posts:
    94
    hey i'm trying to compare my ball color material to a cube color material
    they both have the same albedo color, and i even duplicated the ball color to the cube
    but for some reason my ball keeps getting destroyed. Anyone knows how can i fix this?
    upload_2019-10-16_21-14-56.png
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Next time please use code block and text.

    I would not mix up visual presentation and game logic.

    You could for example use enums, tags, component type or some other way of identifying your gameplay elements like enemy types.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Please don't post screenshots of source code; use code tags instead.

    The documentation for Unity's Color struct does not list an overload of operator==, but I'm pretty sure that C# doesn't allow you to apply that operator to structs unless you've defined it, so...I guess they have one but didn't bother to document it? A Google search turns up several other people who are getting unexpected results from it with no clear explanations, so who knows what it's doing.

    You might try writing your own comparison method that compares the R, G, B, and A properties individually (probably using Mathf.Approximately, because testing floats for exact equality is almost always a bad idea).

    Olmi's suggestion of using your own representation of gameplay category separate from the object's actual color is also worth considering.
     
  4. brunoenvia

    brunoenvia

    Joined:
    Aug 5, 2019
    Posts:
    94
    so icant compare 2 material colors?
     
  5. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    There are discussions about comparing colors out there, here's one that explains some details:
    https://answers.unity.com/questions/787056/comparing-2-color-variables.html

    But as I said, if it's for game logic, I wouldn't compare actual colors. What if you decide to make some art changes and player has different shader, which needs slightly different color to look good... and so on. I can think of many situations where this will cause problems. But I don't know what you are doing.
     
    Last edited: Oct 16, 2019
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I would tentatively say that you shouldn't compare them using the == operator. Clearly you should be able to compare them with that operator (why else would it exist?), but a bunch of people (including you) are finding that it doesn't seem to work correctly in practice, and Unity has no documentation for it, so my conclusion is that it is not trustworthy.

    You can still compare two colors, you just have to do it in a different way.
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Colors are just vectors.
    Subtract them from each other.
    Then make absolute value.
    Then make simple xyzw(rgba) comparison, if greater than 0.01 then is different. Otherwise the same. Obviously you can define a tolerance with setting 0.01 to whatever value you want.

    And yes, please use code tags.