- Home /
Displaying varying text
Hey there. I was just wondering, what is the best way to display varying text to the screen? For example, you you're close to a door then you might see "Press 'E' to open door." But if you're next to a gun, then it says "Press 'E' to pick up." I know how to check distances and how to display text, so that's not the problem. My current method (which I knew would be inefficient) only allows text to be displayed by one object. So if I have two weapons spaced out, I have to find one and pick it up before the other one will display text. What's a good way to go about this? Thanks in advance.
Edit 1: Thanks for the comment, aldonaletto. I tried what you said, but for some reason the function OnTriggerEnter isn't executing. I have the object in a sphere collider marked as a trigger, like you said. For the player, I have a capsule with the tag "Player". I have tried changing the player's collider from a capsule to a box, but that didn't make a difference. As for the code, this is the OnTriggerEnter function:
function OnTriggerEnter(other : Collider) {
Debug.Log("Collisioned!"); //Used to check if anything in the function was being executed.
if (other.tag == "Player") {
gText.text = message;
msgOwner = gameObject;
}
}
Edit 2: Wait, never mind. I looked in the reference manual and I saw that the player should have a Rigidbody component attached to it. Thanks for the help!
Answer by aldonaletto · Mar 30, 2013 at 02:14 AM
I did something like this with triggers and an old and good GUIText: when the player enters a trigger, the trigger script displays a message in a the GUIText; when exiting the trigger, the message text is replaced by an empty string, making the message vanish. When displaying a message, save a reference to the object that posted it - only clear the message when the player leaves this trigger. This can avoid problems when two triggers overlap: when entering the second trigger, its message is displayed, but it must not be cleared when leaving the first one.
Create a GUIText object, name it "Message" and adjust its size, position, etc. then delete its Text property, so that the message will stay invisible until a trigger display its text. In each object that may post a message, add a sphere collider and mark its Is Trigger property, then adjust its size to the desired detection area and finally attach the script below to it, setting My Message in the Inspector to the desired text:
public var myMessage: String; // set the message text in the Inspector
static var gText: GUIText; // reference to the common GUIText object
static var msgOwner: GameObject; // tells who's the current message owner
// the first Start executed find the Message object, and the other triggers
// don't need to find it again:
function Start(){
if (!gText){ // Message not found yet: find it
gText = GameObject.Find("Message").guiText;
}
}
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){ // the player entered the trigger:
gText.text = myMessage; // display its message...
msgOwner = gameObject; // and remember who displayed it
}
}
function OnTriggerExit(other: Collider){
// if the player is leaving the trigger that displayed the message...
if (other.tag == "Player" && gameObject == msgOwner){
gText.text = ""; // clear it
}
}
NOTE: Remember to tag the player as "Player".
Your answer
Follow this Question
Related Questions
Need Text to Display After 5 Seconds 1 Answer
text display on touch 1 Answer
Display GUI Text after delay ? 1 Answer
How to make GUI Text appear after a certain amount of time 2 Answers