How to show image when button is pressed?
I created a simple UI Button called Options and I want to show an image when that button is pressed. How would I do this? This is the code I have, and it's not showing anything when I click on that button.
using UnityEngine;
using System.Collections;
public class Options : MonoBehaviour {
public Texture2D imgOptions;
public void ChangeImage() {
imgOptions = (Texture2D)Resources.Load("Images/PNGs/Menu_Options.png");
}
}
Answer by dhore · Dec 14, 2015 at 01:18 AM
A Texture2D is only a texture, it won't render to the screen by itself. What you're looking for is "Hierarchy > Create > UI > Image" and that will have an component called "Image (Script)" which you can give a "Source Image" to. Then just enable/disable that Image GameObject to show/hide the image on the screen.
Okay I just ended up creating an entirely new scene and using buttons to navigate between them. By any chance would you know how to create a basic AI that follows the user and shoots at them?
I'd suggest learning about Unity's Nav$$anonymous$$esh for AI pathfinding. There's a good Tutorial here.
Your answer
![](https://koobas.hobune.stream/wayback/20220612052509im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scaling an image sent to UI Image 0 Answers
The problem with the UI image. 0 Answers
Displaying GUIText on top of Unity UI Image? 0 Answers
RectTransform offset renders wrong (UnityAndroid) 0 Answers
How to make an object the root of a prefab, or remove the canvas that is set as the root of a prefab 0 Answers