- Home /
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.
Heres my script code so far.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class queueRead : $$anonymous$$onoBehaviour {
public GameObject queue;
public Sprite attack1;
public Sprite attack2;
public Sprite attack3;
public Sprite attack4;
public Image attack1Display;
public Image attack2Display;
public Image attack3Display;
public Image attack4Display;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (queue.GetComponent <battleQueue>().queue[0] == 0)
{
}
}
}
So far so good, though if you're queuing things, you will probably end up using a for/for-each loop.
Can you explain a bit more on what you're trying to achieve? I'm not familiar with the references you mentioned unfortunately.
Answer by Addyarb · Apr 28, 2015 at 11:12 PM
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!
perfect just don't forget to add the using UnityEngine.UI;
worked for me but I referenced a button on another game object.
using UnityEngine.UI;
//Used to change the image of music
public GameObject musicToggleButton;
//must be public as game object start sout off
Image myImageComponent;
public Sprite myFirstImage;
public Sprite mySecondImage;
void Awake()
//Set the button for music so we can change the icon
myImageComponent = musicToggleButton.GetComponent<Image>();
//////////////////////////////////////////////////////////////
/// Toggle the sound. Cycle through all sound modes and set the volume and icon accordingly
public void Toggle$$anonymous$$usic ()
{
if (currentState$$anonymous$$usic == volumeOn)
{
currentState$$anonymous$$usic = volumeOff;
myImageComponent.sprite = mySecondImage;
}
else
{
currentState$$anonymous$$usic = volumeOn;
myImageComponent.sprite = myFirstImage;
}
SetSound();
}
Error: Object reference not set to instance of an object myImageComponent.sprite = myFirstImage;
$$anonymous$$y Error was solved. I removed statement -- myImageComponent = GetComponent(); from Start() and it worked
Answer by epicpython · Oct 15, 2017 at 07:23 PM
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;
Answer by ilozen · Jun 13, 2018 at 10:02 AM
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.
I had a similar situation, this is what I did:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FileFolderWindow : $$anonymous$$onoBehaviour {
public Image report; //change this image
public Sprite[] spri$$anonymous$$rray;//an array of images that will be cycled through
int index = 0;//start at index = 0
void TestNextFunction()
{
index += 1;
if (index > spri$$anonymous$$rray.Length - 1) {//if index is too big
index = 0; //loop back around
}
report.sprite = spri$$anonymous$$rray[index];
}
void TestBackFunction()
{
index -= 1;
if(index < 0)//if index is too small
{ index = spri$$anonymous$$rray.Length - 1;}//loop back around
report.sprite = spri$$anonymous$$rray[index];
}
}
I set the array of images and the public Image report
in the inspector.
Simpler version (no array):
public class FileFolderWindow : $$anonymous$$onoBehaviour {
public Image report; //change this image
public Sprite sprite1;//set in the inspector
void ChangeSprite()
{
report.sprite = sprite1;
}
}
Also note that because the image is set in the inspector, you can attach this to a different object than the image (that's how I did it).
Your answer
Follow this Question
Related Questions
How to create Sprite programmatically 1 Answer
Transparent Images as UI 0 Answers
Check if UI element is fully inside another UI element 1 Answer
Interface relative scaling 0 Answers
Why is texture blurred in one GuiStyle and not in another? 1 Answer