- Home /
GUI Rect not showing on play
Hi,
I am following two different tutorials in order to create an inventory for my 2D game. Both of them use GUI Rect -style approach but whenever I try to test them out but in both cases there is a problem. The GUI doesn't appear anywhere on the screen. I have tried attaching the script to player object, empty object and even GUIText object but the Rect just stays hidden. I have no idea why this happens. Any hints?
Here's one of the scripts I'm trying. It's attached to player.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Inventory2 : MonoBehaviour {
public List<ItemList2> Inventory;
public ItemList2[] Pockets;
Rect inventoryWindow = new Rect (0, 0, 400, 300);
RaycastHit hit;
Ray ray;
void Awake () {
Inventory = new List<ItemList2> ();
}
// Update is called once per frame
void Update () {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetButtonDown("Fire1"))
{
if (collider.Raycast(ray,out hit, 20))
{
print(hit.collider.gameObject);
}
}
}
void onGUI()
{
inventoryWindow = GUI.Window (0, inventoryWindow, DomyWindow, "Inventory");
}
void DomyWindow (int windowId)
{
GUI.DragWindow(new Rect (0, 0, 600, 200));
}
}
The other I'm using is this. The toggle button isn't showing and the Rect stays hidden even if inventoryWindowToggle = true.
{
public Rect inventoryWindow = new Rect (300, 100, 400, 300);
public bool inventoryWindowToggle = false;
static public Dictionary<int, string> inventoryNameDictionary = new Dictionary<int, string>
{
{0, string.Empty},
{1, string.Empty},
{2, string.Empty},
{3, string.Empty},
{4, string.Empty},
{5, string.Empty},
{6, string.Empty}
};
ItemClass1 ItemObject = new ItemClass1 ();
//Likely don't need this one but jsut to be on the safe side...
/*static public Texture2D keyicon;
static public Texture2D bendicon;
static public Texture2D levericon;*/
// Update is called once per frame
void Update () {
}
//The toggle button
void onGUI () {
inventoryWindowToggle = GUI.Toggle (new Rect (800, 50, 100, 50), inventoryWindowToggle, "Inventory");
if (inventoryWindowToggle)
{
inventoryWindow = GUI.Window(0, inventoryWindow, InventoryWindow$$anonymous$$ethod, "Inventory");
}
}
//The inventory screen
void InventoryWindow$$anonymous$$ethod (int WindowId)
{
GUILayout.BeginArea (new Rect (5, 20, 100, 100));
GUILayout.BeginHorizontal ();
GUILayout.Button (inventoryNameDictionary[0], GUILayout.Height (20));
GUILayout.Button (inventoryNameDictionary[1], GUILayout.Height (20));
GUILayout.Button (inventoryNameDictionary[2], GUILayout.Height (20));
GUILayout.Button (inventoryNameDictionary[3], GUILayout.Height (20));
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
GUILayout.Button ("Item 1", GUILayout.Height (20));
GUILayout.Button ("Item 1", GUILayout.Height (20));
GUILayout.Button ("Item 1", GUILayout.Height (20));
GUILayout.Button ("Item 1", GUILayout.Height (20));
GUILayout.EndHorizontal ();
GUILayout.EndArea ();
}
Please convert this to a comment. its not an answer. If you think it is an answer, close the thread as answered :)
you can add debugs to your code to see how far its getting
just add Debug.Log("were here, 1");
tell me how far it gets before it breaks. i think its the if (inventoryWindowToggle) bit. why not just put the GUI toggle line before it as the if statement ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613140359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mysterious crash involving an array 2 Answers
loop GUI Buttons doesn`t respond 0 Answers
Adding Item object to Inventory List 0 Answers
Is there any easy way to keep buttons inside of box? (c#) 1 Answer
Inventory Help 0 Answers