• 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 varunstorm · Dec 26, 2013 at 10:16 AM · javascripttextguitextdisplay

How to display text using javascript ?

How am I supposed to display text when an object is triggered ? Example, I have an object and as soon as the player touches it, some text should be displayed. I have the following code:-

 var mess="Petite";
 function OnTrigggerEnter(other:Collider)
 
 {
         GUI.Label (Rect (10, 30, 1000, 20),"Word: " +mess);
 }
 
 
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 KellyThomas · Dec 26, 2013 at 10:27 AM

`GUI` functions can only be called from `OnGUI()`.

So usually people will manage state in Update or using triggers, then render according to state in OnGUI().

This script could be placed on an item in your level, and will display a message if the player (as identified by the tag system) is within it's trigger collider.

 public var mess: String = "Petite";
 private var inRange: boolean = false;
 
 function OnTriggerEnter(other:Collider) {
     if (other.tag == "Player") {
         inRange =  true;
     }
 }
 
 function OnTriggerExit(other:Collider) {
     if (other.tag == "Player") {
         inRange =  false;
     }
 }
 
 function OnGUI() {
     if(inRange) {
        GUI.Label (Rect (10, 30, 1000, 20),"Word: " +mess);
     }
 }


Comment
Add comment · Show 14 · 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 varunstorm · Dec 26, 2013 at 01:00 PM 0
Share

Thanks, but it didn't work Nothing comes after I enter touch the object I have enabled "Is Trigger" I attached the script to the cube When I go through it, nothing is displayed

avatar image KellyThomas varunstorm · Dec 26, 2013 at 01:02 PM 0
Share

Is your player tagged "Player"?

avatar image varunstorm varunstorm · Dec 26, 2013 at 03:02 PM 0
Share

yes, it is tagged as player

avatar image KellyThomas varunstorm · Dec 26, 2013 at 03:13 PM 0
Share

Sorry I had a typo: "OnTrigggerEnter" with 3 "G"s.

I have corrected the code in my answer and tested it in the application.

Show more comments
avatar image varunstorm · Dec 28, 2013 at 03:13 PM 0
Share

When I increase the text size, the text gets displayed twice.

Why is it so.

This is the code I used-:

 var myStyle = new GUIStyle();
 public var mess: String = "Petite";
 private var inRange: boolean = false;
 
 function OnTriggerEnter(other:Collider) 
 {
     if (other.tag == "Player")
     {
         inRange =  true;
     }
 }
 
 function OnTriggerExit(other:Collider) 
 {
     if (other.tag == "Player") 
     {
         inRange =  false;
     }
 }
 
 function OnGUI() 
 {
     if(inRange)
     {
        myStyle.fontSize=50;
        GUI.Label (Rect (10, 30, 1000, 20),"Word: " +mess,myStyle);
     }
 }

And the text gets displayed like this-:

alt text

Is there any way to make it display only once

Please answer fast

avatar image KellyThomas · Dec 28, 2013 at 05:05 PM 0
Share

@varunstorm now that you have the other question on GUI Text Formatting that is the appropriate place for this follow up question.

Please note that your images are appearing broken in both places so nobody can see what you are talking about.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I display the contents of a class in a Label 0 Answers

changing font + size of text 1 Answer

Problem Displaying Score on GUI text 0 Answers

How to display EULA from Doc in Unity? 0 Answers

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers


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