- Home /
How to make a GUI window appear on clicking a game object
I am new to unity and scripting. I have a 3D character in my game scene and I need a pop up window to appear on clicking the character. Please find the script below.
using UnityEngine;
using System.Collections;
using System;
class PopupTrigger : MonoBehaviour
{
public Rect windowRect = new Rect(20, 20, 120, 50);
void OnMouseDown()
{
OnGUI();
}
void OnGUI()
{
windowRect = GUI.Window(0, windowRect, DoMyWindow, "My window");
}
void DoMyWindow(int windowID)
{
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello..!")) ;
}
}
When I run the game the pop-up appear right away it do not wait for the click. Please help me understanding the problem. ,
OnGUI()
is one of the functions which unity will automatically call - same as Awake()
, Start()
, Update()
, etc.
other functions such as On$$anonymous$$ouseDown()
, for example, get called on certain events without you needing to set anything else up.
you're most likely seeing the message pop up because of that... what you need to do is set a flag when the mouse event occurs then check that in OnGUI()
before displaying (or not, as the case may be) any messages.
Your answer
![](https://koobas.hobune.stream/wayback/20220612063903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Display message when OnMouseDown 2 Answers
OnMouseDown and GUI function 0 Answers
How do I make onMouseDown work for OnGUI in C#? 1 Answer
OnGUI() and key pressing problems. 2 Answers
HUD on Android/iPhone : GUITexture or OnGUI (drawTexture) ? 2 Answers