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
[41090-στιγμιοτυπο-οθονης-3.png|41090]

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);
}
}

}

Hi,

In your KeyControler code, try to declare a public variable who take the image of your Canvas.

public Image keyImage;

41098-key.png

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. :slight_smile:

Ok i found the solution finaly…!! :smiley: When i uploaded the pictures they are set at texture type as textures…i changed that to sprite 2D and everything works fine now… :smiley: Thanks for ur time.

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.

[41291-στιγμιοτυπο-οθονης-4.png|41291]

i followed what u said and smth i am missing and cant find what is it… :frowning:
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.