How do you create a GUI Button upon clicking a game object?

HI i am new to unity and especially new to unity's implementation of java, and i was wondering how i would go about creating a GUI button (4 or 5 of them) after the player clicks a game object.

currently i have an object with an empty game object inside of it set to is trigger. the radius is set to reach outside the parent object. i have 2 scripts attached to it one is for changing the cursor a different color when you are hovering over it (which works fine) the other is supposed to create the GUI buttons after the trigger is clicked.

i have tried going about this in a few ways. to me it seems like i need to use Input.GetMouseButtonDown(0)) but if i try to do it inside a ONGUI() the Buttons are there no matter what. i think this is because you need to call Input.GetMouseButtonDown(0)) in an Update()

so i guess my question is how do i call an update function and then a ONGUI function and get the result im looking for.

Any help at all is well received.

thanks

2 Answers

2

You don't need to use Input.GetMouseButtonDown inside OnGUI. You should set a boolean to true inside Update and check that boolean inside OnGUI.

Something like this:

var selected = false;

function OnMouseUp()
{
    selected = true;
}

function Update()
{
    if (Input.GetMouseButtonDown(0)) //user clicked somewhere else
    {
        selected = false;
    }
}

function OnGUI()
{
    if (selected)
    {
         if (GUI.Button("My Button"))
         {
             // button was clicked
         }
    }
}

The last Answer could not be quite accurate, cus it will show the GUI after clickin anywhere, not only on a game object

the idea of the boolean is correct, but you need code for validating the clicked gameobject

bool showGUIButton =false;
    void Update()
    {
       Ray ray = Camera.main.ScreePointToRay(Input.mousePosition);
       RaycastHit hit;
       if(Physics.Raycast(ray,out hit))
       {
          string whatihit = hit.transform.name;
          //here you can validate the name of what you hit, with the object that you need to  check if you hit it
          //eg.
          if(whatihit == "Player")
          {
             showGUIButton = true;
          }
       }
    }

void OnGUI()
{
   if(showGUIButton)
   {
      if(GUI.Button("Button"))
      {
         //code after the button is clicked
      }
   }
}

hope it helps you =)

My code is correct. OnMouseUp is only called when the object is clicked.

well, about the raycast, i dont remember the places i've been, but is common use for detection of the gameobjects, there are videos on youtube and more, but nothing than a few clicks, a personal recomendation would be tornadotwins on youtube, they have a great tutorial for starters and touch almost every single aspect to make a game, raycast included, but to find it in all those vids will take a while, well, have fun with unity =)

Petroz, as i said, is not quite accurate, if you click in everywhere not neccesary in an object the GUI will be shown, you can click the 1,1 pixel and it would work, even if there is nothing but the gamewindow, it will work, but not as i read on the description "after the player >>>clicks a game object<<<." by the way positive points would be highly appreciated XD