''Image' does not contain a definition for 'color' on Unity
Can't use Color on Image component.
Looked for solution but no luck. How can i use color on Image?
Here is the sample code that should work
public void Locked(GameObject go)
{
Image ima = go.GetComponent<Image>();
// Below line throws 'Image' does not contain a definition for 'color'
ima.color = new Color(200,200,200);
}
Solution 1:[1]
There doesn't seem to be anything wrong with that code. Make sure to include
using UnityEngine.UI;
at the top of your script.
Solution 2:[2]
I don't know why they broke it, but now it works like this: image.material.color=...
Solution 3:[3]
I had this problem with a MonoBehaviour
that uses both namespaces: UnityEngine.UI
and UnityEngine.UIElements
.
The solution is to use full namespace when calling GetComponent<>()
, like this:
img = TheImage.GetComponent<UnityEngine.UI.RawImage>();
img.color = partially_transparent_mix_color; // Renders MiniMap with transparency
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Antoine |
Solution 2 | Alexander Murzin |
Solution 3 | Hatoru Hansou |