how enable image
Hi guys! I’m completely a beginner of this. I was able to connect multiple images to the canvas. By the script below, i would like to achieve, that a given image show up on the canvas by pressing a given button (e.g. between 0-9 buttons). The problem is that no reference can be found to the images. I would ask some help, to fix the problem with reaching the images by the triggering script. The presented script below was used to perform the trigger. As an example (for better insight to the code) it was made only for one button, but of course it still not works with one button neither.
Answer by Mixery322 · Aug 30, 2017 at 11:36 AM
@milhajny So you want do show a Image when pressing ex: 1 and show another when pressing ex:2 ??
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour {
public Image img1;
public Image img2;
void Start () {
// At the start you disable the image 2 and only show the 1
img1.enabled = true;
img2.enabled = false;
}
void Update () {
// If you press 1 you disable the image 2 and enable the 1
if (Input.GetKeyDown(KeyCode.Alpha1))
{
img1.enabled = true;
img2.enabled = false;
}
// If you press 2 you disable the image 1 and enable the 2
if (Input.GetKeyDown(KeyCode.Alpha2))
{
img1.enabled = false;
img2.enabled = true;
}
}
}
In the inspector you will go and put the Image 1 to the img1 variable and the Image 2 img2 and so on.
Hope it works. Good luck!
Answer by HydroxTV · Aug 30, 2017 at 02:39 PM
Mixery´s answer is already good, but as a beginner as you are it might be overwhelming. Your code basically works with the following change:
public Image image;
instead of: private Image image.
In Unity itself (the so called) Inspector you drag and drop your image and youre done :)
But Mixery´s solution will work too!
Your answer
![](https://koobas.hobune.stream/wayback/20220612133827im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to better manage switching out card sprites when loading a card from a deck 1 Answer
Unable to make animations for UI Image 2 Answers
Need help with shield bar enable and disable. 0 Answers
my image wont show up in scene creator 0 Answers
How to resize an image, without affecting the size of the button? 0 Answers