Can't compare colors

Hello everyone, I made a simple if statement comparing the RGB of a color, but it is returning false even when the values correspond. How do I compara colors without this happening?

if(sprite.color.r == 30 && sprite.color.g == 250 && sprite.color.b == 150)
{
     sprite.color = rustic1; //rustic1 is a color
}

I even tried this:

if(Mathf.Approximately(sprite.color.r, 30) && Mathf.Approximately(sprite.color.g, 250) && 
Mathf.Approximately(sprite.color.b, 150))
{
    sprite.color = rustic1;
}

Hello, @pigaroos!

color values are between 0f and 1f, where value 0f means 0 and value 1f means 255, so if you want to compare with range 0…255, you need to divide your values by 255f.

Color color2 = new Color(30/255f, 250/255f, 150/255f, sprite.color.a);

if(sprite.color == color2)
 {
      sprite.color = rustic1; //rustic1 is a color
 }

assuming “sprite” is a SpriteRenderer, it’s color property is a Color struct which stores colors clamped from 0 to 1.

Either use Color32 to compare byte sized colors or account for the difference in your if