Search Unity

menu when click

Discussion in 'Immediate Mode GUI (IMGUI)' started by leogre, Apr 2, 2015.

  1. leogre

    leogre

    Joined:
    Mar 2, 2015
    Posts:
    4
    Hello everyone.
    I am making a card game, and I want my cards to do some actions. I'd like to choose the action they do on a menu that appears when you click on a card.

    I tried that. But the menu doesn't appear when I click on a card.
    I did some test and I'm sure that unity knows when I click on a card, but the menu doesn't appear.

    void update
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if(hit.collider.gameObject.name == name_of_the_card)
    {
    action_menu = true;
    }
    }

    void actionmenu()
    {
    if(action_menu)
    {
    if(GUI.Button(new Rect(100, 200, Screen.width / 2 - 150, Screen.height - 300), "Attack"))
    {

    }
    if(GUI.Button(new Rect(100, 200, Screen.width / 2 - 150, Screen.height - 300), "Close"))
    {
    action_menu = false;
    }
    }
    }
     
  2. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    Do note that you have written "update" with a lowercase 'u'; this function will not get implicitly called by Unity, unlike its brother(/sister?) with an uppercase "U". Futhermore, I don't see the function "OnGUI", but I assume you call the "actionMenu" function from there. This is some sort of keypart: the legacy GUI system does not work outside of the GUI loop.

    On another note, both buttons seem to be on the exact same position. As a result they will be drawn over each other, resulting in the close button taking effect. This should be easy to fix, and is not the problem at hand.

    Lastly, why not use the new UI system introduced in Unity 4.6? It is easier to design a nice looking UI with it, and is availible for free in all Unity versions ever since its release...