- Home /
Enable image on mouse click
Hi! I was trying to make a script, but i am new to it and i don't know exactly how to enable and disable a UI image, so i made this script:
#pragma strict
//Below is my image variable.
var Img : UI.Image;
function Update () {
if (Input.GetKeyDown ("Fire2"))
Img.SetActive (true);
//But i get the error "SetActive is not a member of "UnityEngine.UI.Image".
//What is wrong? =P
}
I don't know what is wrong... The strange this about this is that i searched a lot and i only found questions with no answer or questions using C#, but i have choosen to use Javascript because it is more compatible
Answer by JScotty · Oct 07, 2016 at 10:02 AM
Your problem is that UI.Image is not a gameobject but an script, you could use .setActive if you use it like:
var img : GameObject;
function SetImageActive(){
img.SetActive(true);
}
ps. please start your variable names with lowercase.. https://en.wikipedia.org/wiki/Coding_conventions
Answer by sandeepsmartest · Oct 06, 2016 at 04:46 AM
I have c# version.
using UnityEngine.UI;
public Image Bg1;
void Awake()
{
Bg1.enabled = false;//Untested code
}
Hope this may help you. Nsks
.enabled is the same for js aswel ;)
your case:
#pragma strict
//Below is my image variable.
var img : UI.Image;
function Update () {
if (Input.Get$$anonymous$$eyDown ("Fire2"))
img..enabled = true; // enabling image script
}
https://docs.unity3d.com/ScriptReference/Behaviour-enabled.html (right corner of page enable js if c# is enabled)
Your answer
Follow this Question
Related Questions
Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer
how to allow the key to only open 1 door rather than all of them? 0 Answers
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer
First Person Controller - Problems with footstep audio being played too fast. 0 Answers