unity set image code example

Example 1: unity image

//Make sure you are using UnityEngine.UI
using UnityEngine.UI

//We can assign this image in the inspector
public Image image;
//Also assign this in the inspector
public Sprite mySprite;

public void ChangeImage () 
{
	image.sprite = mySprite;
}

Example 2: unity change the source image or image

public Image test;
public Sprite example;

void Start()
{
 test.sprite = example; //change the source image of the image "test" to the sprite "example" 
}

Example 3: change image runtime unity

Image[] images = PlayerBarExample.GetComponentsInChildren<Image>();         Image face = images[0];         foreach (Image image in images)         {             if (image.gameObject.CompareTag("face"))                 face = image;         }