- Home /
How can I change color of an Image in script (png source) ?
I made a png image with gimp with one color and transparent. I have include this image in Image UI component in unity. I linked my image to my script, as usual. But I find many ways to change the color on many forums (mainly answers.unity) but none of these work in my project : I know the first one is only if I declared my image as gameObject.
How can I do the color change ?
The correct way to do it is to assign the stepCircle.color (3rd option). What is the problem? From what you show, it should work, so maybe the problem comes from another part of your code?
$$anonymous$$aybe I have a clue : when I go to the unity inspector and change the color like this, it doesn't work either. The only way to change is to add a material, the image disappear for most of all.
Is your image black by any chance? Color property is multiplicative in Unity so changing it for black (0, 0, 0) images does nothing as anything times 0 is still 0
Indeed, my color was black (0, 0, 0). I try with red and blue color, and each color is 0 by default can't change. So I repaint all my images to white. Thank you very much, it works
Another possibility I can think of is if you set the source image within your code, using "myImage.sprite = aSprite;". Then, sometines (I'm not sure if it's a bug or not), changing the other parameters of the image have no effect. A solution is to use "myImage.overrideSprite = aSprite;" ins$$anonymous$$d.
Answer by yanndelescaille · Aug 05, 2019 at 12:35 AM
Thanks to @Casiell for the answer. Thanks to @SirPaddow too but I don't try the the sprite.
In the case of peoples can't read the comments, I put the answer here :