How to make a UI Image appear/disappear?
I want to press the key "I" on the keyboard and have an image appear and after pressing "I" on my keyboard again, the image will disappear.
This might be way more obvious than I think it is, but I've looked everywhere for hours and can't find anything.
C# or Javascript, I'm not picky!
Answer by yapaysinek · Apr 20, 2016 at 09:38 PM
Successfully tested and works (for me) ;))
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ImageShow : MonoBehaviour {
public bool isImgOn;
public Image img;
void Start () {
img.enabled = true;
isImgOn = true;
}
void Update () {
if (Input.GetKeyDown ("i")) {
if (isImgOn == true) {
img.enabled = false;
isImgOn = false;
}
else {
img.enabled = true;
isImgOn = true;
}
}
}
}
It worked! I just gave up after attempting to write a bunch of scripts. Thanks! I really appreciate this, my game should be finished by tonight thanks to you!
Glad I could help! Good luck with the project :)
Can't I just select the image by script? Or do I have to manually select it?!
Heyo! First comment evar, yay. Just wanted to say you can do this even quicker using "!", which means "NOT" in C#. It's really useful for such little toggles because it allows you to change the value of the boolean to whatever it is not at the moment like so:
if (Input.GetKeyDown(KeyCode.T))
{
img.enabled = !img.enabled;
}
i want to make it so that when i hold the button, the image disappears. this just toggles it on/off
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class HideOnKeyHold : $$anonymous$$onoBehaviour
{
public Image image;
public KeyCode key;
void Update ()
{
img.enabled = !Input.GetKey(key);
}
}
Answer by fatihcamci · May 20, 2017 at 08:48 AM
ı have a canvas with uı object but ı cant enabled and disable them.The are some question in my project.For example first question is true ı want to disable it and enable second one.I am looking for this questions for afew days but i couldnt find it :(((
i wrote these: void tiklandiDogru() {
GameObject.Find("Soru_Dragon").SetActive(false);
//above code works but below not works.i wonder at first Soru_Tiger is not active.it is not works because of this state.
GameObject.Find("Soru_Tiger").SetActive(true);
//gameObject.SetActive(!gameObject.activeSelf);
}
Your answer
Follow this Question
Related Questions
Cant Disable Script on Another Gameobject 1 Answer
Why are Unity events so special? 1 Answer
Why doesn't enabling and disabling not work? 1 Answer
Disabling a certain image 3 Answers