Unity 5.0 Trying to Change the Image - Source Image via Script

So the title says a big portion of what I am trying to do.
I am trying to use an external script to change the Source image of the UI > Image
Although I dont quite know what variable to use.

I am making a GUI feed back system for a battle queue. Sort of like Biowares game Dragon Age or Star Wars Knights of the old republic. So I can queue the attacks. I already got the attack queue working so I just need this as visual feedback.

Your help would be greatly appreciated.

Hi there! Sorry to hear you’re having trouble swapping out images.

The Unity 4.6UI+ system is a bit different for sure. For one thing, the “Image” component uses sprites, which is a little confusing. However, here is how to properly switch them out.

using UnityEngine;
using UnityEngine.UI;

public class ImageTestClass : MonoBehaviour {
    // Drag your image component here in the inspector window
    public Image myImageComponent;

    // Drag your first sprite here in the inspector window
    public Sprite myFirstSprite;

    // Drag your second sprite here in the inspector window
    public Sprite mySecondSprite;

    // Call this method somewhere to set your image's sprite to myFirstSprite
    public void SetImage1() {
        myImageComponent.sprite = myFirstSprite;
    }

    // Call this method somewhere to set your image's sprite to mySecondSprite
    public void SetImage2() {
        myImageComponent.sprite = mySecondSprite;
    }
}

Hope this has helped you. Good luck, and feel free to ask if you have any questions!

In Unity 5.6, you have to use this.GetComponent<SpriteRenderer>().sprite= myfirstImage; instead of

 myImageComponent = GetComponent<Image>(); //Our image component is the one attached to this gameObject.

 myImageComponent.sprite = myFirstImage;

I have the same case but I don’t have a SpriteRenderer so the solution provided by @epicpython does not work for me. I just have an Image directly placed, so it has a CanvasRenderer.

Any idea on how to get this to work?

Edit:
Found the solution! I had to change both Image.sprite and Image.overrideSprite and then it started updating properly.