- Home /
Display GUI when function is called
How can you get a GUI to appear when a function is called. none of the examples in the scripting referance show how to do this. This is what I have so far.
var windowRect : Rect = Rect (20, 20, 120, 50);
function OnTriggerEnter (player)
{
Debug.Log("NPC contact");
NPC_GUI();
}
function Start ()
{
player = GameObject.Find("player");
}
function NPC_GUI()
{
// Create GUI
GUI.Box (Rect (10,10,200,90), "Test");
}
The game runs fine but when NPC_GUI is called I get the error.
ArgumentException: You can only call GUI functions from inside OnGUI. UnityEngine.GUIUtility.CheckOnGUI () UnityEngine.GUI.Box (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) UnityEngine.GUI.Box (Rect position, System.String text) dialog.NPC_GUI () (at Assets/Standard Assets/Scripts/my scripts/dialog.js:20) dialog.OnTriggerEnter (System.Object player) (at Assets/Standard Assets/Scripts/my scripts/dialog.js:8)
I think a workaround would be to add a script to a object when the gui is needed then take it away. is there a better way?
Answer by robertbu · Sep 30, 2013 at 01:54 AM
I think you want something like this:
var windowRect : Rect = Rect (20, 20, 120, 50);
private var showGUI = false;
function OnTriggerEnter (player)
{
Debug.Log("NPC contact");
showGUI = true;
}
function Start ()
{
player = GameObject.Find("player");
}
function OnGUI()
{
// Create GUI
if (showGUI)
GUI.Box (Rect (10,10,200,90), "Test");
}
Answer by getyour411 · Sep 30, 2013 at 01:51 AM
In OnGUI
If (somestate is_true)
GUI.Box (Rect (10,10,200,90), "Test");
some other code sets somestate to false to turn off the GUI component.
Your answer
Follow this Question
Related Questions
How do I create a GUI on collision detection? 1 Answer
GUI.Window function with variables? 3 Answers
Button is unable to grab functions with two parameters? 0 Answers
function GUI.DrawTexture with OnMouseEnter/Exit to change texture all within script? 0 Answers
Variable won't change, any suggestions? 2 Answers