- Home /
How to have OnGUI element in a OnTriggerEnter function?
Hi, how can i to have GUI functions inside a OntriggerEnter function? because we cant put a function inside a function....please help on what other approach...thanks
Answer by aldonaletto · Aug 20, 2011 at 07:57 PM
You can't. You must use a boolean variable to enable GUI functions and set/reset it at OnTrigger, like this:
EDITED: As I told, you can't put OnGUI inside OnTriggerEnter - you must create your GUI code inside a regular OnGUI function, and use a boolean variable to enable your GUI code; set or clear this variable at OnTriggerEnter/OnTriggerExit, and you will get the same effects as if the GUI code were inside OnTrigger:
var guiEnable = false; // variable which controls GUI
function OnGUI(){ if (guiEnable){ // your GUI code goes here } }
function OnTriggerEnter (myTrigger : Collider) { if (myTrigger.name == "user") guiEnable = true; // enable gui code on trigger enter }
function OnTriggerExit (myTrigger : Collider) { if (myTrigger.name == "user") guiEnable = false; // disable gui code on trigger exit }
Thanks for reply, but I still get error saying that "You can only call GUI functions from inside OnGUI". and BTW,how do I put the OnTriggerEnter condition like below with the code you give?thanks alot
function OnTriggerEnter (myTrigger : Collider) { if(myTrigger.gameObject.name == "user"){ } "I want OnGUI function on here but cant" }
But you can't place GUI functions inside OnTrigger: take a look at my answer (I edited it to adapt to your code). The GUI functions are actually placed inside the OnGUI function, but only execute if the variable guiEnabled is true - and this variable is set/reset in OnTrigger events.
Ohh Thanks, I understand the concept you trying to say. However, I dont know why the compiler still give me the same error. The error is "ArgumentException:You can only call GUI function inside OnGUI". Even that the GUI function is called under the OnGUI declaration as in the example given by you. I'm sorry, I'm not very good in javascript. Thanks alot
Its ok, I manage to get it to work....thanks alot... you really helped me alot.....
Sorry for being too late, but how did you get it to work? I am facing the same issue. Even after correcting the code, it wont work. :(
Answer by cengizhangokben · Jul 16, 2012 at 01:15 PM
This should be your solution;
function OnTriggerEnter(collision : Collider)
{
if (collision.tag == "Player")
{
sart = true;
Application.LoadLevel("///////////");
}
}
function OnGUI()
{
if(sart)
{
GUI.BeginGroup(new Rect(Screen.width/2+450, Screen.height - 100,290,90), "");
GUI.Box(new Rect(10,40,200,40), "");
//////////////////////////////////
GUI.EndGroup();
}
}
Answer by kubawich · Apr 25, 2014 at 10:35 AM
I have something like that using UnityEngine; using System.Collections; public class RenderinGUI : MonoBehaviour { public GUITexture texture; public GUIText text; bool on = false; void Start(){ text.enabled = false; texture.enabled = false; } void OnTriggerEnter(Collider col){ if (col.tag == "Player") { on = true; } } void OnGUI(){ if (on == true) { text.enabled = true; if (Input.GetKey (KeyCode.E)) { text.enabled = false; texture.enabled = true; } if (Input.GetKeyUp (KeyCode.E)) { text.enabled = true; texture.enabled = false; } } } }
Your answer
Follow this Question
Related Questions
Error: expecting ( 1 Answer
force trigger to detect at x time 0 Answers
Collecting 2 items at a time by mistake. 0 Answers
Jump function not working 1 Answer
my enemy is broken 1 Answer