Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 rahib · Mar 30, 2010 at 02:42 PM · guigameobjecttext

How to make text appear when moving next to an object?

i am trying to make a script that makes text appear when you move next to an object. i have used the GUI text but i want it to appear when you move close to an object and dissapear when you move away from the object. is it possible to do this?

please help thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by e-bonneville · Mar 30, 2010 at 03:07 PM

Yes, that's possible. Here's what you do (assuming you are using JS). Create a sphere gameobject, and set it's collider to trigger. Remove the mesh renderer, and position it so that its trigger collider surrounds the gameObject. Next, put this script on it:

function OnTriggerEnter (other : Collider) { if (other.CompareTag ("Player")) { SendMessageUpwards("SetTheGUI"); } }

function OnTriggerExit (other : Collider) { if (other.CompareTag ("Player")) {
SendMessageUpwards("UnSetGUI"); } }

Next, create another script, and put it on your main character:

var close = false; var textFieldString = "text field";

function SetTheGUI () { close = true; }

function UnSetGUI () { close = false; }

function OnGUI () { if (close) { GUI.Label (Rect (25, 25, 100, 30), textFieldString); } }

Now, if your player enters the trigger you made, it will set a boolean variable inside your player script to 'true'. If the value is true, then a GUI text field will appear. Hope this helps!


EDIT: If it's still not working, go over this list, making sure they are all right.

  1. Make sure your collider is surrounding the object - and make sure it's big enough.
  2. Is it set to be a trigger?
  3. Re-copy the code into two seperate files. Save them, and put the first on your trigger, and put the second on your player.
  4. Is your first player controller's tag set to "Player"?


ANOTHER EDIT: Set your trigger's code to this:

function OnTriggerEnter (other : Collider) { SendMessageUpwards("SetTheGUI"); }

function OnTriggerExit (other : Collider) {
SendMessageUpwards("UnSetGUI"); }

Hmm. I really can't see anything else that's wrong in my instructions... It all should work. Try one more time, except replace the old trigger code with this above code. Also, recopy the player's code, I made a couple of changes.

Comment
Add comment · Show 11 · 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 rahib · Mar 30, 2010 at 04:13 PM 0
Share

do you add the second script to the first person controller or to an object? thanks.

avatar image e-bonneville · Mar 30, 2010 at 04:17 PM 0
Share

You add it to the first person controller(main character, I called it)

avatar image rahib · Mar 30, 2010 at 04:32 PM 0
Share

does'nt seem to work :(. do i need to create GUI text?

avatar image e-bonneville · Mar 30, 2010 at 04:39 PM 0
Share

Hmm. $$anonymous$$ake sure your first person controller's tag is player. $$anonymous$$aybe I've got a typo. I'll go over it again.

avatar image rahib · Mar 30, 2010 at 04:59 PM 0
Share

yeah ive done evrything you've said except now when i walk into trigger the game pauses.

Show more comments
avatar image
1

Answer by Christophe F · Apr 28, 2010 at 10:55 AM

Elbon96 did a fantastic job here.

Addition is that the Sphere Object needs to be a child of your main character's object, so that the "SendMessageUpwards()" can be received.

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 orx · Sep 28, 2012 at 01:55 PM

you need to creat a sphere collider (compomponent> physics> sphere collider. whn the object Selected) set it to "is trigger". Then Attach this code to the player:

var displayMessage = false;

function OnTriggerEnter (other : Collider) {

displayMessage = true; }

 function OnGUI () {
 if (displayMessage){
   GUI.Label (Rect (10, 10, 100, 20), "Hello World!");
   }
   }

*javascript

this code Does that any trigger you go to, It writes "hllow world" if you want its for Specific object I think you need to Write:

function OnTriggerEnter (other : Collider "object name") {

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

1 Person is following this question.

avatar image

Related Questions

How to save and load text in a GUI TextArea 1 Answer

Quest Script Help 2 Answers

Adding a script component through scripting 1 Answer

Simple Backpack "Pick up and drop" 1 Answer

How to create a gui button inside an if statement? 1 Answer


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