- Home /
 
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");
 }
 
              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.
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
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 :)
Answer by townsendmedia · May 15, 2014 at 06:38 PM
thank you for the script! It worked for me with a little tweaking. CT
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
 
              Your answer
 
             Follow this Question
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