• 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
Question by Guzaii · Oct 28, 2013 at 02:58 PM · javascript

GUI.Box that will appear for 3sec

I'm trying to make a GUI.Box appear for 3 seconds, when entering a Collider (For like Zones and such). But i seem to not be able to understand the timer. Here's what i've got so far:

 var guiOn : boolean;
 var guiSkin : GUISkin;
 var zoneName : String;
 var size : Rect = Rect(0,0,100,100);
 var textTimer : float = Time.deltaTime * 2;
 static var deltaTime : float;
 
 
 function Start () {
 
 
 
 }
 
 function Update () {
 
 if(textTimer==2){
 textTimer +1;
 }
 
 }
 
 function OnTriggerEnter(theCollider : Collider) {
 
     guiOn=true;
     
 }
 
 function OnGUI () {
     
     GUI.skin = guiSkin;
     
     if(guiOn) {
         GUI.Box(size,zoneName);
     }
 }

I'm still trying to make it work myself, but it would be AWESOME with some help :)

Comment

People who like this

0 Show 0
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
Best Answer

Answer by ArkaneX · Oct 28, 2013 at 04:14 PM

Basically you need to reset your timer at trigger enter, increase it inside Update, and display text if it is less or equal than 3 seconds. So something like this:

 function Update() {
     if(textTimer <= 3) {
         textTimer += Time.deltaTime;
     }
 }
  
 function OnTriggerEnter(theCollider : Collider) {
     textTimer = 0;
 }
 
 function OnGUI() {
     if(textTimer <= 3) {
         GUI.Box(size,zoneName);
     }
 }
Comment

People who like this

0 Show 9 · 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 Guzaii · Oct 28, 2013 at 04:22 PM 0
Share

Thanks :D it works :)

avatar image Guzaii · Oct 28, 2013 at 04:33 PM 0
Share

I dont think it'll work anyway.. i just tested it.. and it seems like once i face away from the object with the collider.. then the timer resets (as if i didnt enter it)

avatar image ArkaneX · Oct 29, 2013 at 09:47 AM 0
Share

If the timer resets, then it looks like the collision was triggered again (unless you set timer to 0 anywhere else in your code).

Additionally, code from my answer is just a simple example, and you should probably modify it to suit your needs. For example, I don't know how exactly you're using your script - if you dynamically add it to a game object, and timer is initialized to 0, then your GUI will be visible. To prevent this, you have to add some additional conditions.

avatar image Guzaii · Oct 29, 2013 at 11:44 AM 0
Share

The change you added works fine, and just the way i wanted it to work. But once i face away from the object with the script, then it resets...

avatar image ArkaneX · Oct 29, 2013 at 12:03 PM 0
Share

So it looks like your player object exited collision area for a moment and then entered it again. Is your player collider rectangular, with length greater than width? In this case when you collided, rotating can cause OnTriggerExit and then OnTriggerEnter again.

Show more comments

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

16 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Unity Javascript debug error stating BCE0044: expecting :, found ';'. Any help? 2 Answers

Show "Score:5" on my screen, how? 1 Answer

Finding if a specific key contains a specific value in a Hashtable? 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