Search Unity

GUI.Button Problems

Discussion in 'Immediate Mode GUI (IMGUI)' started by Adam_Smestad, Apr 27, 2016.

  1. Adam_Smestad

    Adam_Smestad

    Joined:
    Apr 27, 2016
    Posts:
    4
    (Hopefully posting to right thread, let me know if not)

    Making a game for school in unity and I'm in charge of making a trade menu/interface to have the player trade items in their inventory with an NPC. I made a C# script and attached it to the NPC object on the scene. I've got the inventory coming up with the menu so the player can see what is in their inventory. The problem comes in when I press the "OK" or "Cancel" button. The button is pressed and nothing happens. When you leave the collider with the NPC, the menu exits, but when you interact with the NPC again, the buttons show up, but they are not clickable. Here is the code, any suggestions are appreciated! Keep in mind that it isn't finished, just ran into a bump I can't seem to get over.

    *Edit: I'm just working on getting the "Cancel" button working so the "OK" button isn't suppose to do anything yet.
    *Edit: Also, sorry about spamming this, wasn't too sure how the forums worked.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. public class Trading : MonoBehaviour
    7. {
    8.     public Text activateText;
    9.  
    10.     private Inventory playerInventory;
    11.     private GameObject other;
    12.     private ItemDatabase database;
    13.     private List<Rect> itemSlots = new List<Rect>();
    14.     private GUISkin skin;
    15.     private bool showMenu = false;
    16.     private bool activated = false;
    17.  
    18.  
    19.     // Initialization of activateText and player inventory
    20.     void Start()
    21.     {
    22.         playerInventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
    23.         database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
    24.         activateText.text = "";
    25.     }
    26.  
    27.     // Update is called once per frame and checks if "e" was pressed
    28.     void Update()
    29.     {
    30.         if (activated && Input.GetKeyDown(KeyCode.E))
    31.         {
    32.             //For Testing<<<<<<
    33.             //playerInventory.AddItem(1);
    34.             //playerInventory.AddItem(1);
    35.             //>>>>>>
    36.             showMenu = true;
    37.             OnGUI();
    38.         }
    39.     }
    40.  
    41.     // Displays text cue when the player enters the item's sphere collider
    42.     void OnTriggerEnter(Collider other)
    43.     {
    44.         // check if the colliding object is a 'Player'
    45.         if (other.gameObject.CompareTag("Player"))
    46.         {
    47.             activateText.text = "Do you want to trade? (Press E)";
    48.             activated = true;
    49.             this.other = other.gameObject;
    50.         }
    51.     }
    52.  
    53.     // Removes the activateText when the player exits the collider
    54.     void OnTriggerExit(Collider other)
    55.     {
    56.         if (other.gameObject.CompareTag("Player"))
    57.         {
    58.             activateText.text = "";
    59.             activated = false;
    60.             this.other = null;
    61.             playerInventory.showInventory = false;
    62.             showMenu = false;
    63.         }
    64.     }
    65.  
    66.     void OnGUI()
    67.     {
    68.         if (showMenu)
    69.         {
    70.             GUI.WindowFunction errorWindow;
    71.             activateText.text = "";
    72.             playerInventory.showInventory = true;
    73.  
    74.             GUI.Box(new Rect(Screen.width - 500, Screen.height - (Screen.height - 50), 300, 200), "Trade\n\n\n\n&                   ----------->");
    75.  
    76.             if (this.gameObject.CompareTag("Wood_trade"))
    77.             {
    78.                 itemSlots.Add(new Rect(Screen.width - 275, Screen.height - (Screen.height - 100), 50, 50));//, "", skin.GetStyle());
    79.                 itemSlots.Add(new Rect(Screen.width - 400, Screen.height - (Screen.height - 100), 50, 50));//, "", skin.GetStyle());
    80.                 itemSlots.Add(new Rect(Screen.width - 475, Screen.height - (Screen.height - 100), 50, 50));//, "", skin.GetStyle());
    81.             }
    82.  
    83.             Rect outline = new Rect(Screen.width - 100, Screen.height - (Screen.height - 100), 250, 250);
    84.  
    85.             foreach (Rect ele in itemSlots)
    86.             {
    87.                 GUI.Box(ele, "", playerInventory.skin.GetStyle("Slot"));
    88.             }
    89.  
    90.             if (GUI.Button(new Rect(Screen.width - 425, Screen.height - (Screen.height - 200), 50, 30), "OK"))
    91.             {
    92.                 /*int count = 0;
    93.                 if (this.gameObject.CompareTag("Wood_trade"))
    94.                 {
    95.                     for (int i = 0; i < playerInventory.inventory.Count; i++)
    96.                     {
    97.                         for (int j = 0; j < database.items.Count; j++)
    98.                         {
    99.                             if (database.items[j].itemID == 1)
    100.                             {
    101.                                 count++;
    102.                             }
    103.                         }
    104.                     }
    105.  
    106.                     //item numbers are for testing atm
    107.                     if (count > 1)
    108.                     {
    109.                         playerInventory.RemoveItem(1);
    110.                         playerInventory.RemoveItem(1);
    111.                         playerInventory.AddItem(2);
    112.                         showMenu = false;
    113.                         playerInventory.showInventory = false;
    114.                     }
    115.                     else
    116.                     {
    117.                         showMenu = false;
    118.                         playerInventory.showInventory = false;
    119.                         GUI.Window(0, new Rect(Screen.width - 425, Screen.height - (Screen.height - 200), 50, 30), errorWindow, "Error, you don't have the required items");
    120.                         if(GUI.Button(new Rect(Screen.width - 425, Screen.height - (Screen.height - 200), 50, 30), "OK"))
    121.                         {
    122.                             GUI.enabled = false;
    123.                         }
    124.                     }
    125.                 }*/
    126.             }
    127.             if (GUI.Button(new Rect(Screen.width - 325, Screen.height - (Screen.height - 200), 50, 30), "Cancel"))
    128.             {
    129.                 playerInventory.showInventory = false;
    130.                 showMenu = false;
    131.             }
    132.         }
    133.     }
    134. }
     
    Last edited: Apr 27, 2016