- Home /
RGB values of object color is not working properly
Hi! I want to be able to compare the color of an object with another color stored on the script of the object. The problem is that when i try to compare them, no matter what color the object has in the beginning, when i debug the RGB, values are shown as 1. So even if the start color of the object is red, it will always show in my script as white.
public class CheckColor : MonoBehaviour {
[SerializeField] Color otherColor;
private void Start() { Color thecolor = GetComponent().material.color;
Debug.Log(thecolor.r);
Debug.Log(thecolor.g);
Debug.Log(thecolor.b);
}
}
What do you expect this line to do: GetComponent().material.color
It's not really a good way to do anything and I can't even really tell what this would really give you.
What did you want to achieve? Do you want to get the color of a sprite? Or do you really need the color of a material?
I don't know why it doesn't let me update it, but yes it should be GetComponent SpriteRenderer ().material.color
SpriteRenderer has it's own color field, so you probably want to access that ins$$anonymous$$d of the material
Answer by TomazTreven · Dec 16, 2019 at 08:01 AM
Probably because by default float gets rounded when converted to string. Something like (manually specifying string format):
Debug.Log(thecolor.r.ToString("F3"));
Will probably do the job