Reputation: 23
Hello i have i problem with sprtirerender colors. I have the same Color set in the inspector for both gameobjects
But when I am checking the colors in debug.log the colors are not the same anymore
I'm not changing colors in the code anywhere.
Color goColor = gameObject.GetComponentInChildren<SpriteRenderer>().color;
Debug.Log(gameObject.name +" = "+ goColor);
foreach (GameObject g in go)
{
Color color = g.GetComponentInChildren<SpriteRenderer>().color;
Debug.Log(g.name + " = " + color);
//Checkar om färgen stämmer överens med sin connection
if (color == goColor)
{
objectConnection = g;
Debug.Log(gameObject.name + "Connectade till: " + g);
}
else
{
//Debug.Log("Hittade inga matchningar");
}
}
Upvotes: 0
Views: 393
Reputation: 90580
I think the issue here is the way how this color picker and Color
works.
As you can see internally Color
is actually stored using float
components 0f
to 1f
.
If you configure the Color via the Hex value or the 0-255
option these values are converted internally and stored as float
s.
Then further the implementation of Color.ToString
rounds the value for better readability to a precision of 3 decimals.
For visualizing I made a simple
Debug.Log($"rounded: {color}");
Debug.Log($"actual: {color:G9}");
Debug.Log($"hex: {ColorUtility.ToHtmlStringRGBA(color)}");
So actually when you configure all colors via the option RGB 0 - 1
for your printed float
values you get
for Buttons
rounded: RGBA(0.271, 0.858, 0.712, 1.000)
actual: RGBA(0.270999998, 0.85799998, 0.712000012, 1)
hex: 45DBB6FF
and for Blockade
rounded: RGBA(0.275, 0.859, 0.714, 1.000)
actual: RGBA(0.275000006, 0.859000027, 0.713999987, 1)
hex: 46DBB6FF
note especially the last line: While in your screenshot the hex value would be 45DBB5
, configuring the colors via the float values actually produces 46DBB6
and 45DBB6
so there is a certain range within the float value might move around a little while the hex / bytes value stays still the same (which makes sense of course since bytes of course have way less precision than a float).
However, Color == Color
works with an underlying conversion to Vector4 == Vector4
and the precision for that is 0.00001
which is way more precise than makes sense on a color level.
As a solution to your case you could rather go less precise and simply configure exactly that: The hex value
if(ColorUtility.ToHtmlStringRGBA(color) == ColorUtility.ToHtmlStringRGBA(goColor))
Upvotes: 1