Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Different · Aug 17, 2012 at 02:59 PM · triggerguitexttouchingsign

Show text on colision

I need some type of sign, when if player touched trigger, the GUI text is showing, and when player is not touching it, the GUI text is gone. Here's my current script:

 var ShowText : boolean = false;
     var center : Vector2;
     center.x = Screen.width / 2;
     center.y = Screen.height / 2;
     var bw = 100; //width (for posting at center)
     var bh = 50; //height (for posting at center)
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.gameObject.CompareTag("Player"))
     {
         ShowText = true;
         GUIText (center.x - (bw/ 2),center.y - (bh / 2),bw,bh, GUIwillshow);
     }
 }
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

2 Replies

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

Answer by nikescar · Aug 17, 2012 at 03:23 PM

Something like this should work for you:

 var colliding : boolean;
 function OnGUI()
 {
     if (colliding)
     {
         GUI.BeginGroup(Rect(Screen.width/2, Screen.height/2, 100, 100));
         GUI.Label(Rect(0,0,100,100), "Hit");
         GUI.EndGroup();
     }
 }
 
 function OnTriggerEnter(col : Collider)
 {
     if (col.gameObject.CompareTag("Player"))
     {
         colliding = true;
     }
 }
 
 function OnTriggerExit(col : Collider)
 {
     colliding = false;
 }
 

Or if you want to use GUIText, just set the "text" variable in the inspector to your pre-placed GUIText and use this code:

 var text : GUIText;
 var colliding : boolean;

 function Update()
 {
     if (colliding)
     {
        text.enabled = true;
     }
     else
     {
         text.enabled = false;
     }
 }
 
 function OnTriggerEnter(col : Collider)
 {
     if (col.gameObject.CompareTag("Player"))
     {
        colliding = true;
     }
 }
 
 function OnTriggerExit(col : Collider)
 {
     colliding = false;
 }
Comment
Add comment · Show 4 · 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 Different · Aug 17, 2012 at 03:28 PM 0
Share

Looks good, no errors, but the GUI text is not showing. $$anonymous$$aybe I'm doing something wrong? I've attached script to the sign. I can just change in the inspector "Coliding" true and false.

avatar image nikescar · Aug 17, 2012 at 04:18 PM 0
Share

I've edited the answer with an option for using GUIText if you'd rather go that route. Also, the "colliding" variable should probably have a "private" in front of it so it does show in the inspector.

Alternatively you should be able to place the text.enabled / text.disabled directly in the OnTrigger functions.

avatar image Different · Aug 17, 2012 at 05:38 PM 0
Share

2nd script looks better, but still no results. It hides/shows the GUI text, but only when I'm changing this in the Inspector, not when I'm touching sign :/

avatar image nikescar · Aug 17, 2012 at 05:42 PM 0
Share

This script should be attached to the sign. The script checks to see if the object it is attached to is colliding with an Object with the "Player" tag.

avatar image
1

Answer by Weitzel · Aug 17, 2012 at 03:26 PM

So, a pattern I've used is to create an object (like a sphere) around the object you want to interact with, that has a collider (with trigger checked).

Separate the behaviour so it can be independant of the dialogue. You can later abstract the dialogue to multiple types easily if you need to. The collision object would have a script attached only has one concern:

 var signText : String;
 
 private var dialogue;
 function Update () {
 }
 
 function OnTriggerEnter(other : Collider) {
     if(other.gameObject.GetComponent(playerTag) != null)
         startDialogue();
 }
 
 function OnTriggerExit(other : Collider) {
     if(other.gameObject.GetComponent(playerTag) != null)
         stopDialogue();
 }
 
 function startDialogue() {
     dialogue = gameObject.AddComponent(simpleDialogueObject);
         dialogue.setTextToDisplay(signText);
 }
 
 function stopDialogue() {
     Destroy(dialogue);
 }


where your "simpleDialogueObject" would looke something like:

 private var signText  = "";
 
 function OnGUI () {
     GUI.Box (Rect (Screen.width * 0.05 ,Screen.height * 0.5, Screen.width * 0.9 , Screen.height * 0.45), signText);
     
 }
 
 function setText(newText : String){
         signText = newText;
 }
Comment
Add comment · Show 4 · 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 Different · Aug 17, 2012 at 03:33 PM 0
Share

Hmm. It's showing me 3x $$anonymous$$ identifier. Trigger is on, by the way.

Gah, it's hard to be newbie.

avatar image Weitzel · Aug 17, 2012 at 03:46 PM 0
Share

So this is pseudo code, and the unknown identifier is probably because its doing checks against class names that are different from what you may or may not have defined. The more pointed your question, the more I can help.

It is hard being a noob, stick with it and keep asking very useful, helpful, and pointed questions like these, they help.

avatar image Different · Aug 17, 2012 at 03:54 PM 0
Share

What can I else say? Player is named First Person Controller (how original), sign is named sign, text should appear on the center, JS would be nice... That's all.

Thanks for help anyway, I'll try some different ways.

avatar image Weitzel · Aug 17, 2012 at 04:07 PM 0
Share

when you say something like

gameObject.AddComponent(simpleDialogueObject);

it will try to add a script named "simpleDialogueObject" to the gameObject you are talking about. if you don't have a scripted named this, it will claim it is an "unkown identifier" when it tries to compile. even if capitalization is off.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Trigger Script Not Firing 4 Answers

Need help making an audio trigger. 2 Answers

How to trigger a Script from an Object. 0 Answers

Trigger to make another trigger to appear? 2 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