- Home /
Get the color component from a raw image UI
Hello, I've been trying to get the color component from a raw image so that I can change its alpha, but I seem to be having some trouble. HealthColor is the variable that I am storing the color in, HealthImage is the "Raw Image (Script)" component that I dragged into a public variable slot from the inspectors view of my HealthBar, which is the actual UI object found on the canvas.
HealthColor = HealthImage.GetComponent<Color> ();
ArgumentException: GetComponent requires that the requested component 'Color' derives from MonoBehaviour or Component or is an interface. UnityEngine.GameObject.GetComponent[Color] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:38) StatsManagerScript.Update () (at Assets/Scripts/StatsManagerScript.cs:38)
Answer by FlyingHighUp · Oct 14, 2017 at 05:01 AM
HealthColor = HealthImage.color;
That is not working.
Assets/Scripts/Stats$$anonymous$$anagerScript.cs(43,29): error CS1061: Type UnityEngine.Component' does not contain a definition for
color' and no extension method color' of type
UnityEngine.Component' could be found. Are you missing an assembly reference?
The HealthImage variable is probably declared like
public Component HealthImage;
It should be
public RawImage HealthImage;
After making this change, you'll have to re-drag the RawImage component over the field.
It turns out that I was not able not have RawImages as variables in my script because I did not declare it with "using UnityEngine.UI" first. I can now see the alpha dwindling down in the inspector's panel for my Stats$$anonymous$$anger object when I start the game, but it is not affecting the HealthBar in the UI. At this point I'd rather just have one variable related to changing the color and alpha of the HealthBar.
HealthBar.RawImage.Color.a = 0.5;
Your answer
![](https://koobas.hobune.stream/wayback/20220612141002im_/https://answers.unity.com/themes/thub/images/avi.jpg)