How to have a GUI Texture pop up after clicking a button?

I’m trying to find out how to get a clipboard to appear when I click a button. I have the clipboard texture as a GUI Texture, and it’s on the screen when the game is playing. How do I hide it until I press the button (which is one of the Unity default buttons), and then have it hide?

#pragma strict
// JavaScript
var icon : Texture2D;// Defines icon textures
var icon1 : Texture2D;// Defines icon textures
var icon2 : Texture2D;// Defines icon textures
var icon3 : Texture2D;// Defines icon textures
var icon4 : Texture2D;// Defines icon textures;
 
function OnGUI () {
    GUI.Button (Rect (10,10,100,21), GUIContent ("Tools", icon, "Open tools"));// Makes Tools button
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    GUI.Button (Rect (115,10,100,21), GUIContent ("Chart", icon1, "View patient's chart"));//Makes Chart button
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    GUI.Button (Rect (220,10,100,21), GUIContent ("Interact", icon2, "Interact with patient"));
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    if (GUI.Button (Rect (10,700,120,21), GUIContent ("End Simulation", icon3, "Quit")))// Makes Quit button
       Application.Quit();// Quits simulation if button is pressed
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    GUI.Button (Rect (325,10,100,21), GUIContent ("Help", icon4, "Get some help"));// Makes Help Button
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
 
 
}

I would want the button that says “Chart” to display the picture.

Do you have a bit of source code for us to help with? The short version is to add a boolean that is toggled when the user clicks a button. Then, when OnGUI iterates and sees that boolean is true, it will be render the clipboard texture.

Yeah. I put the buttons in their own scripts. Here's the one for the chart. #pragma strict var icon1 : Texture2D; function OnGUI () { GUI.Button (Rect (115,10,100,21), GUIContent ("Information", icon1, "View patient's chart"));//Makes Chart button GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does }

if(GUI.Button (Rect (115,10,100,21), GUIContent ("Information", icon1, "View patient's chart"));//Makes Chart button){ showToolTip=true; } if(showToolTip){ GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does } And to hide in in the future you can start a coroutine witch would turn the showToolTip false, but this will render your button useless or at least you will only know what does it do when you already clicked it.

There's a compiler error saying that showToolTip is an unknown identifier.

Yeah you need declare that :P if you are using csharp then private bool showToolTip = false; OR if Javascript: private var showToolTip : boolean = false;

1 Answer

1

K, now I get what you want, this is the full script you need to implement:

private var showToolTip : boolean = false;
public var yourTexture: Texture;
function OnGUI () {
  if(GUI.Button (Rect (115,20,100,21), GUIContent ("Information", icon1, "View patient's chart"));//Makes Chart button){
    showToolTip=true;
  }
  if(showToolTip){
    GUI.DrawTexture(Rect(115,0,100,20), yourTexture);//this will draw a texture above your button
  }
}

Attach this script to an empty game object and drag your texture to the Your Texture slot in the inspector window

Edit: Implemented in your script:

    #pragma strict
    // JavaScript
    var icon : Texture2D;// Defines icon textures
    var icon1 : Texture2D;// Defines icon textures
    var icon2 : Texture2D;// Defines icon textures
    var icon3 : Texture2D;// Defines icon textures
    var icon4 : Texture2D;// Defines icon textures;

    private var showToolTip : boolean = false;
    public var yourTexture: Texture;

    function OnGUI () {
    GUI.Button (Rect (10,10,100,21), GUIContent ("Tools", icon, "Open tools"));// Makes Tools button
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    if(GUI.Button (Rect (115,10,100,21), GUIContent ("Chart", icon1, "View patient's chart"))){
          showToolTip=!showToolTip;
      }
 if(showToolTip){
    GUI.DrawTexture(Rect(115,0,100,20), yourTexture);//this will draw a texture above your button
  }
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    GUI.Button (Rect (220,10,100,21), GUIContent ("Interact", icon2, "Interact with patient"));
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    if (GUI.Button (Rect (10,700,120,21), GUIContent ("End Simulation", icon3, "Quit")))// Makes Quit button
    Application.Quit();// Quits simulation if button is pressed
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
    GUI.Button (Rect (325,10,100,21), GUIContent ("Help", icon4, "Get some help"));// Makes Help Button
    GUI.Label (Rect (10,40,100,35), GUI.tooltip);// Shows description of what button does
     
     
    }

Alright, looks good so far! Thanks, and have a nice day!

You're welcome :) But an accept answer wouldn't hurt if this solved the problem, so others would know it's answered. (A thumbs up wouldn't hurt but that's not necessary :D)