- Home /
 
Displaying text on trigger enter
Hello everyone, I have a question for you all today. I have been trying to display some text telling a player to turn back or be killed so that they don't fall off the map, but I am having some trouble actually displaying it on the screen. My code so far is as follows:
 using UnityEngine;
 using System.Collections;
 public class Boundaries : MonoBehaviour 
 {
     //Declaring variables
     bool crossedBoundary;
     void OnTriggerEnter(Collider other) 
 {
     //Telling game to actiavte boolean
     if(other.gameObject.tag == "Player") 
     {
         crossedBoundary = true;
     }
 }
 void OnGUI () 
 {
     //If the boolean is active, display the text
     if (crossedBoundary == true) 
     {
         GUI.Label(Rect(200, 100, 0, 0), "Turn back or die!");
     }
 }
 }
 
              Another tip while we're here, you only need to write the name of the bool, to check if true. Like this:
 if (crossedBoundary)
 {
 dostuff();
 }
 
                  And just use an ! if you want it to check if not true. (!crossedBoundary).
Answer by Invertex · Jan 09, 2014 at 02:40 AM
Well, one clear issue is that you forgot to put "new" before the "Rect" call. It should be:
  GUI.Label(new Rect(200, 100, 0, 0), "Turn back or die!");
 
              Also, make sure you have the player object tagged as Player.
Thanks, I'll try this as soon as I have time. While you guys are still here, what would you recommend I do for the countdown timer until the Player actually dies? I've heard something about coroutines, but I don't really understand what they are. Again, thanks for your help (can't believe one word caused me to stay up for 2 hours trying to figure out what was wrong with my script!)
Coroutines run separate from the Update function, and in them, you can declare Wait times, such as "`yield return new WaitForSeconds(10.0f);`". You'd set up a coroutine by making a new method in your script, starting with "IEnumerator" then "name you want" and () encapsulation. So:
 IEnumerator playerWait(){
 do stuff;
 yield return new WaitForSeconds(10.0f);
 do more stuff;
 }
 
                  It has a lot of uses. I'd suggest reading up more on them :)
http://unitygems.com/coroutines/
(Oh, and you'd initiate the Coroutine from somewhere else in your script, when you wanted to, with StartCoroutine(playerWait()); Assu$$anonymous$$g your coroutine was named "playerWait".) 
Your answer
 
             Follow this Question
Related Questions
Need help with scripting bug fix. pleease 0 Answers
C# GameObject is not detecting collision with Character Controller 2 Answers
Collision between capsule and sphere colliders does not work 1 Answer
C# Plane Collision Detection 1 Answer
OnCollisionStay only detects collisions after a secondary collision 0 Answers