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 Aug749 · Jul 24, 2013 at 08:47 PM · timerwaitforsecondsdisappearkeycode

disappear script that last a certain amount of time

i have created a guitext that i have programmed to turn off when i press one of two different buttons. they both turn it off for as long as i hold that button, so if i dont it will just flash off then on again. My problem is that for one of the two key commands i need a timer the tell it how long to shut off for. I believe i am very close to the final script i just am not sure what i need to do. Im pretty sure i need to set another var or two but not entirely. Any and All help is appreciated!

Normal script:

 var go : GameObject;
 
 function Update () {
     
     go.SetActive(!Input.GetKey(KeyCode.LeftShift));
     go.SetActive(!Input.GetKey(KeyCode.R));
 }

My FAIL edit:

 var go : GameObject;
 
 function Update () {
     
     go.SetActive(!Input.GetKey(KeyCode.LeftShift));
     
     if(Input.GetKey(KeyCode.R)){
     Reload();
     }
 }
 
 function Reload () {
         go.SetActive(!Input.GetKey(KeyCode.R)) = true;
         yield WaitForSeconds(2);
         go.SetActive(Input.GetKey(KeyCode.R)) = false;
 }
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
0

Answer by wolfadex · Jul 25, 2013 at 01:57 AM

The problem was that when you released 'R', 'LeftShift' was not pressed so the guiText was set to active. This should work better:

 #pragma strict
 var hiddenTime = 2;
 var releaseTime = 0;
 
 function Start () {
 
 }
 
 function Update () {
 
     if (Input.GetKey(KeyCode.LeftShift)) {
         gameObject.guiText.enabled = false;
         releaseTime = -hiddenTime;
     }
     
     if (Input.GetKey(KeyCode.R)) {
         gameObject.guiText.enabled = false;
         releaseTime = Time.time;
     }
     
     if ( Time.time - releaseTime >= hiddenTime ) {
         gameObject.guiText.enabled = true;
     
Comment
Add comment · Show 3 · 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 Aug749 · Jul 25, 2013 at 04:42 PM 0
Share

makes sense, thank you ill be trying this when i get a chance

avatar image Aug749 · Jul 25, 2013 at 10:18 PM 0
Share

doesn't work

avatar image wolfadex · Jul 25, 2013 at 10:52 PM 0
Share

$$anonymous$$y new code works, I tested it.

avatar image
0

Answer by Grim_Darknight · Jul 25, 2013 at 02:24 AM

You can set a timer using time.deltaTime * 1.

 function Update()
 {
   go.SetActive(!Input.GetKey(KeyCode.LeftShift));
     
   if(Input.GetKey(KeyCode.R))
   {
     timer = 0;  // this will set the timer variable to 0 as long as R is pressed
   }
 
   if (timer < visibilityTimer)
   {
     go.SetActive(false);
     timer += (time.deltaTime * 1); // if the timer variable is less than visibility timer variable it will increase 1 unit per second.
   }
   else
   {
     go.SetActive(true);
   }
 }


For this to work you will need to set up 2 variables:

 private var timer : float;
 var visibilityTimer : int;

the timer variable must be a float or it won't work, the visability timer variable can be float or int; also if you set visibility timer to public you can customize the timing from the inspector.

Comment
Add comment · Show 3 · 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 Aug749 · Jul 25, 2013 at 04:42 PM 0
Share

thanks ill be trying this when i get a chance!

avatar image Aug749 · Jul 25, 2013 at 10:20 PM 0
Share

your script makes sense, however when i go to press the other button, it does not dissappear, and wehen the character spawns the gui turns off for 2 seconds then on. i can solve the shift key button with using my old script seperatley, but the spawn error is what gets me

avatar image Aug749 · Jul 25, 2013 at 10:26 PM 0
Share

actually no it isn't, either way i cant get the shift key to make the gui disappear

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

17 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

Related Questions

How do I...? [scripting help] 2 Answers

Skipping over timer 1 Answer

SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers

Countdown Timer or WaitForSeconds? 2 Answers

Objects won't disappear after a set time after collision 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