GUI.Button Problems

(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. 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.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class Trading : MonoBehaviour
{
    public Text activateText;

    private Inventory playerInventory;
    private GameObject other;
    private ItemDatabase database;
    private List<Rect> itemSlots = new List<Rect>();
    private GUISkin skin;
    private bool showMenu = false;
    private bool activated = false;
 

    // Initialization of activateText and player inventory
    void Start()
    {
        playerInventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
        database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
        activateText.text = "";
    }

    // Update is called once per frame and checks if "e" was pressed
    void Update()
    {
        if (activated && Input.GetKeyDown(KeyCode.E))
        {
            //For Testing<<<<<<
            //playerInventory.AddItem(1);
            //playerInventory.AddItem(1);
            //>>>>>>
            showMenu = true;
            OnGUI();
        }
    }

    // Displays text cue when the player enters the item's sphere collider
    void OnTriggerEnter(Collider other)
    {
        // check if the colliding object is a 'Player'
        if (other.gameObject.CompareTag("Player"))
        {
            activateText.text = "Do you want to trade? (Press E)";
            activated = true;
            this.other = other.gameObject;
        }
    }

    // Removes the activateText when the player exits the collider
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            activateText.text = "";
            activated = false;
            this.other = null;
            playerInventory.showInventory = false;
            showMenu = false;
        }
    }

    void OnGUI()
    {
        if (showMenu)
        {
            GUI.WindowFunction errorWindow;
            activateText.text = "";
            playerInventory.showInventory = true;

            GUI.Box(new Rect(Screen.width - 500, Screen.height - (Screen.height - 50), 300, 200), "Trade\n\n\n\n&                   ----------->");

            if (this.gameObject.CompareTag("Wood_trade"))
            {
                itemSlots.Add(new Rect(Screen.width - 275, Screen.height - (Screen.height - 100), 50, 50));//, "", skin.GetStyle());
                itemSlots.Add(new Rect(Screen.width - 400, Screen.height - (Screen.height - 100), 50, 50));//, "", skin.GetStyle());
                itemSlots.Add(new Rect(Screen.width - 475, Screen.height - (Screen.height - 100), 50, 50));//, "", skin.GetStyle());
            }

            Rect outline = new Rect(Screen.width - 100, Screen.height - (Screen.height - 100), 250, 250);

            foreach (Rect ele in itemSlots)
            {
                GUI.Box(ele, "", playerInventory.skin.GetStyle("Slot"));
            }

            if (GUI.Button(new Rect(Screen.width - 425, Screen.height - (Screen.height - 200), 50, 30), "OK"))
            {
                /*int count = 0;
                if (this.gameObject.CompareTag("Wood_trade"))
                {
                    for (int i = 0; i < playerInventory.inventory.Count; i++)
                    {
                        for (int j = 0; j < database.items.Count; j++)
                        {
                            if (database.items[j].itemID == 1)
                            {
                                count++;
                            }
                        }
                    }

                    //item numbers are for testing atm
                    if (count > 1)
                    {
                        playerInventory.RemoveItem(1);
                        playerInventory.RemoveItem(1);
                        playerInventory.AddItem(2);
                        showMenu = false;
                        playerInventory.showInventory = false;
                    }
                    else
                    {
                        showMenu = false;
                        playerInventory.showInventory = false;
                        GUI.Window(0, new Rect(Screen.width - 425, Screen.height - (Screen.height - 200), 50, 30), errorWindow, "Error, you don't have the required items");
                        if(GUI.Button(new Rect(Screen.width - 425, Screen.height - (Screen.height - 200), 50, 30), "OK"))
                        {
                            GUI.enabled = false;
                        }
                    }
                }*/
            }
            if (GUI.Button(new Rect(Screen.width - 325, Screen.height - (Screen.height - 200), 50, 30), "Cancel"))
            {
                playerInventory.showInventory = false;
                showMenu = false;
            }
        }
    }
}

Any reason you are using OnGUI instead of the new UI system? OnGUI will give you a ton of headaches and has a bunch of gotchas waiting for new users.

Still new to unity and this was something I found online so I thought it would work. I did think of a different way to do this. around though. Thanks for replying though! I will differently remember to learn up on it if I want to make a game in the future.

void Update()
    {
            OnGUI();
        }
    }

This is not how to use OnGUI. It is an event that Unity itself will call for you. Comparable to the Update() itself.

1 Like