- Home /
How to change the color of an image with script?
I want to change the color of a image in my scene with a script and it gives me this error. I'm using Unity5. How can I fix this?
image.color = new Color32(255,255,255,100);
error CS1061: Type UnityEngine.GameObject' does not contain a definition for
color' and no extension method color' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Answer by DiegoSLTS · Jun 28, 2015 at 12:01 AM
Your "image" variable is of type GameObject and the GameObject class doesn't have a color property or member (that's what the error message is telling you), it must be an Image variable. Try this:
image.GetComponent<Image>().color = new Color32(255,255,225,100);
s.ta.c. Useless info. e all know where unity's help and it doesn't help.
Good job DiegoSLTS.
That helped. Good job.
Here is my code.
public Image timerIconBar;
if (timerIconBar) { timerIconBar.GetComponent().color = new Color32(255, 255, 0, 255); }
$$anonymous$$ake sure to include using UnityEngine.UI;
at the top.
Answer by starikcetin · Jun 27, 2015 at 11:59 PM
I understand from error that you declare "image" as a GameObject. You need to declare it as an Image.
Follow the link below for more information about Image class:
Your answer
Follow this Question
Related Questions
Object Reference Error On Camera Script 1 Answer
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
Why do I get this error while trying to create a color? 2 Answers
Error message from console after destroying an Object with sound Logical Error ? 1 Answer