I have a white image , I want when I click on a button the image color becomes dark, how can I do this?
Attach this script to the button. Then drag and drop your button gameobject in “On Click” section of the same button
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour {
public void Click()
{
GetComponent<Image>().color = Color.black;
}
}
If this is a UI Image (or Raw Image), you can change its 2 property.
You can already try this in the editor. If this is what you expect, then you can do this with a simply method.
using UnityEngine.UI;
[RequireComponent (typeof (Image))]
public class ImageAttributes : MonoBehaviour
{
Image image;
void Awake ()
{
image = GetComponent<Image>();
}
public void ChangeColor (Color col)
{
image.color = col;
}
}
Then on your button, you can add this method as a listener of the onClick event, and choose a color.
Just select your button, then in its properties go to that “pressed color” and change that.