- Home /
Unity 4.6 UI - change image's source image via script
Hi,
I have buttons for levels, that have an image component. I set the image using the editor to the default "level unlocked" image. Now in the script, I am looping through all the levels, and for each, I read a playerprefs string associated with that level to determine if it was unlocked. If the level was not unlocked, I want to change the source image of that component to a "level locked" image.
I tried to set the image name to my new image's name (clearly that failed atrociously)
gameObject.transform.GetChild(j).GetComponent<Image>().sprite.name = "my_new_image";
I tried having for each level two buttons, one representing 'locked' and one representing 'unlocked', disabling one and enabling the other. That worked, but I really am not liking the style to do it, and would prefer a solution that would fix the first approach.
Thanks a lot
Answer by Zephrim · Sep 20, 2014 at 04:50 AM
What's wrong?
You need to be changing the actual Sprite Object, rather the the Sprite Object's name. The Sprite is the actual "Image" of the Image UI Component in Unity 4.6.
How do I fix it?
While I can't exactly talk you through it word for word, I can give you my example with comments.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TestMenu : MonoBehaviour
{
/// <summary>
/// This is the Sprite we're going to swap to.
/// </summary>
/// <remarks>
/// This is set in Unity's Inspector.
/// </remarks>
public Sprite OtherSprite;
/// <summary>
/// The Array of Images.
/// </summary>
Image[] images;
// Use this for initialization
void Start()
{
// Get all components of type Image that are children of this GameObject.
images = gameObject.GetComponentsInChildren<Image>();
// Loop through each image and set it's Sprite to the other Sprite.
foreach (Image image in images)
{
image.sprite = OtherSprite;
}
}
}
Hi, i ended up using part of your solution since the GetComponentsInChildren wasn't my main problem. But having a public Sprite[] that contains the images I want (i initially set them in the inspector) worked beautifully. Thanks!
@Zephrim maybe you can help me? i've been trying to change the sprite of a UI image for ages, should be simple like changing a texture... but it isn't, i've used you're answer but i get the following error:
The type `Image' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method `UnityEngine.GameObject.GetComponentsInChildren<T>()'
i'm using unity 4.6, in the middle of a project so have not upgraded to 5
thank you I hope you, or someone can help!
@Carrel Is there any way you could paste the code here so I can have a look at it?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class YourScript: $$anonymous$$onoBehaviour {
public int x;
public Sprite red;
public Sprite green;
public Sprite blue;
// Use this for initialization
void Start () {
x = Random.Range (0, 2);
if (x == 0)
gameObject.GetComponent<Image> ().sprite = red;
if (x == 1)
gameObject.GetComponent<Image> ().sprite = green;
if (x == 2)
gameObject.GetComponent<Image> ().sprite = blue;
}
// Update is called once per frame
void Update () {
}
}
with this you can change the sprite w/o changing the whole object. I tried this on Unity5 btw.
Answer by Thalthanas · Mar 23, 2015 at 09:40 PM
You can also change the sprite w/o changing the whole object like:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class YourScript: MonoBehaviour {
public Sprite red;
public Sprite green;
public Sprite blue;
void Start () {
x = Random.Range (0, 2);
if (x == 0)
gameObject.GetComponent<Image> ().sprite = red;
if (x == 1)
gameObject.GetComponent<Image> ().sprite = green;
if (x == 2)
gameObject.GetComponent<Image> ().sprite = blue;
}
}
Answer by epicpython · Nov 24, 2016 at 04:27 AM
Hey, so if gameObject.GetComponent<Image> ().sprite = blue;
isn't working for you, try gameObject.GetComponent<SpriteRenderer> ().sprite = blue;
It worked for me, hopefully this helps someone else. :)
Answer by aeldron · Nov 23, 2017 at 02:00 PM
For anyone still looking for this answer:
m_Image = GetComponent<Image>();
m_Image.sprite = myNewSprite;
https://docs.unity3d.com/ScriptReference/UI.Image-sprite.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613163356im_/https://answers.unity.com/themes/thub/images/avi.jpg)