Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Different · Feb 28, 2013 at 10:02 AM · guitrigger

Show GUI.Label when touching trigger

So yeah, I'm trying to make something like this: When player is touching the invisible box (no collider). GUI.Label shows on the screen. When he's leaving the trigger, the label hides. The problem is that I should use function OnTriggerEnter, but for GUI I should use OnGUI. Any clue what I should do? Scripts:

     var Guiscript : image1 ;
     
     function OnTriggerEnter (other : Collider)) {
     if (Guiscript.enabled == true){
     Guiscript.enabled = false;
     }
     else {
     Guiscript.enabled = true;
     }
     }
     }

image1 script:

 function OnGUI () {
 GUI.Label (Rect (10, 300, 100, 20), "TEXT");
 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by Mikilo · Feb 28, 2013 at 10:06 AM

Hi!

Use a boolean to toggle the OnGUI. This boolean must be set in the OnTriggerEnter()/Exit().

 var toggleGUI : bool; // Set it in the event Enter/Exit
 function OnGUI () {
 if (toggleGUI == true)
     GUI.Label (Rect (10, 300, 100, 20), "TEXT");
 }

Good luck.

Comment
Add comment · Show 9 · 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 Different · Feb 28, 2013 at 10:11 AM 0
Share

Thanks! :)

avatar image Mikilo · Feb 28, 2013 at 10:15 AM 0
Share

You're welcome. =D

avatar image Different · Feb 28, 2013 at 10:26 AM 1
Share

Okay, managed to write this script.

     var toggleGUI : boolean;
     
     function OnTriggerEnter (other : Collider) {
     toggleGUI = true;
     }
     
     function OnTriggerExit (other : Collider) {
     toggleGUI = false;
     }
     
     function OnGUI () {
     if (toggleGUI == true)
     GUI.Label (Rect (10, 300, 100, 20), "TEXT");
     }

It works! :D

avatar image Mikilo · Feb 28, 2013 at 10:30 AM 1
Share

Wasn't that simple? You even merge your scripts.

avatar image danielng01 · Oct 10, 2013 at 02:08 PM 0
Share

Thanks :)

Show more comments
avatar image
0

Answer by Wingman526 · Jan 26, 2014 at 08:25 PM

Ok never mind it was my fault. I finally figured it out. I had some bodies of dead soldiers within the trigger area and this was setting it off on game start. Thank you for your help regardless :)

Comment
Add comment · Show 1 · 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 Mikilo · Jan 26, 2014 at 08:34 PM 0
Share

You're welcome. =D

avatar image
0

Answer by townsendmedia · May 15, 2014 at 06:38 PM

thank you for the script! It worked for me with a little tweaking. CT

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

Answer by crod310 · Dec 10, 2014 at 06:26 PM

This can work for any guitext you have in the object

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Text;
 using System.IO;
 
 public class Showtext : MonoBehaviour {
     GUIText guiText;
     // Use this for initialization
     void OnTriggerEnter(Collider other) {
         guiText.enabled = true;
     }
     void OnTriggerExit(Collider other) {
         guiText.enabled = false;
     }
 a

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How To Make GUI Buttons Load/Quit 1 Answer

GuiTexture Width Change 1 Answer

[Solved] GUI Error? 1 Answer

Display GUI texture on specific variable value? 0 Answers


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