Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chocolateyap · Aug 20, 2011 at 07:41 PM · errortriggerfunctiononguideclaration

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chocolateyap · Aug 20, 2011 at 08:12 PM 0
Share

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" }

avatar image aldonaletto · Aug 20, 2011 at 08:32 PM 0
Share

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.

avatar image chocolateyap · Aug 20, 2011 at 08:40 PM 0
Share

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

avatar image chocolateyap · Aug 20, 2011 at 10:57 PM 0
Share

Its ok, I manage to get it to work....thanks alot... you really helped me alot.....

avatar image Minay · Jul 03, 2014 at 05:58 PM 0
Share

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. :(

avatar image
0

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();
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

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; } } } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges