- Home /
How to update UI image mid-game (Unity, C#)
Hello, I'm trying to update a UI image when you click on different buttons. I am currently able to change the UI image only at the start of the game by using C# scripts to set the UI image's source image to something. I believe I have written the correct code that adds a layer of image changing to change the UI image - basically, I'm able to change the image of the image that directly changes the image of the UI image. When I plug this line of code into Start() it works perfectly. When I put it in a function and assign a button to fire the program when it is clicked on, it doesn't work. It also doesn't work when I plug the code into Update() which makes me think that my code only allows for the image change at the start of the game. Here is my code: " public Sprite FrogImage;
void Start()
{
}
public void FClickCharacter()
{
GameObject.FindWithTag("ChoosedCharacter").GetComponent<SelectedCharacter>().sprite1 = FrogImage;
}
public void MClickCharacter()
{
}
public void PClickCharacter()
{
}
public void VClickCharacter()
{
}
" FrogImage is an image I set in Unity, and "SelectedCharacter" is a different script containing "sprite1" which changes the UI image. When I assign FClickCharacter to a button and click the button it doesn't work. When I put it into Start() the UI image is set to "FrogImage" at the start of the game. When I put it into Update() it just is the original value of sprite1. Any suggestions or tips would be very appreciated, thanks!
Answer by mak431020 · Aug 21, 2021 at 05:42 AM
First you have to declare a Image and find the gameobject sprite in the start or awake then store the gameobject sprite in the image. Then you can do what you want
Image frogSprite;
void Start()
{
frogSprite = GameObject.FindWithTag("ChoosedCharacter").GetComponent<SelectedCharacter>().sprite1;
}
public void FClickCharacter()
{
frogSprite = FrogImage;
}
Answer by Infenix · Aug 21, 2021 at 07:39 AM
Frm what I see in you code, I'm actually surprised you get it working in the start method. If I understand well, sprite1 is a member of the SelectedCharacter class. You could use a property in the SelectedCharacter class to update the image when you set the sprite value like this.
using UnityEngine;
using UnityEngine.UI;
public class SelectedCharacter : MonoBehaviour
{
private Image image;
private Sprite sprite1;
public Sprite Sprite1{
get => sprite1;
set {
sprite1 = value;
image.sprite = sprite1;
}
}
void Awake() {
image = GetComponent<Image>();
}
//Rest of your code
}
This way, you can now assign the value in your function with this call
public void FClickCharacter()
{
GameObject.FindWithTag("ChoosedCharacter").GetComponent<SelectedCharacter>().Sprite1 = FrogImage;
}
It should automatically update the image sprite when changing the value. If it doesn't work, you should check in the scene view when being in play mode if you have a character with the ChoosedCharacter tag and if this object is enabled. If not, the Find method will fail.
Your answer
Follow this Question
Related Questions
How to change UI image with a name to a different image with a name 2 Answers
Why we have to use getcomponent instead of a simple reference ? 2 Answers
Meaning of .text 1 Answer
Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer
HasComponent 4 Answers