- Home /
How to enable and disable raw image on ui system
Hello everyone, I have made a canvas with a raw image at the top right corner. My image is a key, what i want to do is when i pick up the real key, in my main game, to show up the picture at the corner and when i use the key to unlock a door for example the image to be disabled from the corner so i cant see it.
I have a picture that shows the canvas at the top and the main game at the bottom. So as u can see i dont want the image to be shown at the start of the game but when u pick up the key
Here is my key control code which is a simple counter to know if i got the key or not and with o collider so the player can destroy it:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class KeyController : MonoBehaviour {
public GameObject keygot;
void Start(){
}
void OnTriggerEnter(Collider collider){
if (collider.gameObject.name == "Player"){
PublicVariables.KeyCount+=1;
Destroy (gameObject);
}
}
}
Answer by Flazone · Feb 20, 2015 at 01:31 PM
Hi,
In your KeyControler code, try to declare a public variable who take the image of your Canvas.
public Image keyImage;
Make a code who enable this image when you pickup the key and disable it when you use it.
keyImage.enabled = true;
or
keyImage.enabled = false;
This is just a proposition, I think there is many ways to do that. :)
Hi, Thanks for your answer, i have already tried that before but i cant drag and drop the image on the canvas when i make a bublic image.I can only drag the picture if my public variable is a gameobject and if i do that the enable and disable command wont work. :(
Hi,
You need to use the UI 4.6 Image component :
GameObject -> UI -> Image
I made a little script that makes disappear an image if you press E
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class test : $$anonymous$$onoBehaviour {
public Image testImage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E)){
testImage.enabled = false;
}
}
}
You just need to drag/drop your image on the public variable in the inspector. It works for me !
Answer by kostasandre · Feb 24, 2015 at 12:17 PM
Ok i found the solution finaly..!! :D When i uploaded the pictures they are set at texture type as textures...i changed that to sprite 2D and everything works fine now.. :D Thanks for ur time.
Oh well! Sorry I didn't think about that..
Good luck for the rest!
Answer by kostasandre · Feb 24, 2015 at 12:17 PM
Hello again and thanks for your time. As u said i made GameObject ->UI ->Image and followed ur instructions my problem again is that for a reason i cant drag and drop my image if my variable is public Image ,i can only do that if i set it as public GameObject.
i followed what u said and smth i am missing and cant find what is it.. :( From my assets im trying to drag and drop the key2 picture to the key Image inspector which is set as public image KeyImage.
*When i first created gameObject->UI->RawImage i could drag and drop it right on the canvas at my scene as the first post ive made,now i cant either do that.