- Home /
Gui button doesnt show
heres my code:
using UnityEngine;
using System.Collections;
public class barrackSpawner : MonoBehaviour
{
public Texture2D txture;
void Start()
{
}
void OnGUI()
{
Event e = Event.current;
if(e.isMouse)
{
Debug.Log("Barracks Menu Open");
// Make a background box
if(GUI.Button(new Rect(15,15,100,50),txture))
Debug.Log("Gui works");
}
}
}
the button doesnt display but i do get the debug message when i click on the right area. i did search but didnt find anyone with similar problem.
Answer by robertbu · Aug 14, 2013 at 04:33 PM
The Button code needs to be executed during a EventType.Repaint to show. So to show you can do:
if(e.isMouse || e.type == EventType.Repaint)
What are you trying to do with restricting your button to mouse events?
i want to pop-up gui button when the barracks on the screen is clicked
I don't know what a 'barracks' is. Assu$$anonymous$$g it is the game object this script is attached to, then what you want to do:
At the top of the file:
bool showButton = false;
New function:
void On$$anonymous$$ouseDown() {
showButton = true;
}
Then your GUI code:
void OnGUI()
{
if(showbutton && GUI.Button(new Rect(15,15,100,50),txture)) {
Debug.Log("Gui works");
showButton = false;
}
}
thanks that worked!!! another question can i use the same for GUI group?
Enclose your whole group inside an 'if (show)' statement.
thanks for the solution, can you tell me what i was doing wrong in the OP? The logic is somewhat similar...
Your answer
![](https://koobas.hobune.stream/wayback/20220613112217im_/https://answers.unity.com/themes/thub/images/avi.jpg)