• 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 Gantar180 · Mar 30, 2013 at 01:39 AM · guitextdisplayweaponsdoors

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!

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

1 Reply

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

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

How do you implement a script like this one... 1 Answer

Display a Text/GUI on screen when triggerd with Fadein/Fadeout 1 Answer

Need Text to Display After 5 Seconds 1 Answer

text display on touch 1 Answer

Display GUI Text after delay ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges