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 WaterMinecraft · Dec 04, 2013 at 07:00 PM · key pressed

How to create a countdown counter based off of a Key press.

I have been taking my hand at creating a timer that countsdown when the letter 'f' is pressed. I only want it to count down when this letter is pressed, (EDIT: I mean this to mean that it starts a countdown when you click F, and resets either when the timer ends, or the player hits f) because I am creating a timer that when it reaches 0 the light turns off. (: I've tried creating a timer, so I will post it up!

 private var timeLeft: float = 0;
 var totalTime : float = 30;
 
 function Awake(){
 timeLeft = totalTime;
 }
 
 function Update(){
 timeLeft -= Time.deltaTime;    
 if(timeLeft < 0)
     {
     light.enabled = false;
             
     }
 }

Thank you! (: Also, this is just excerpts from my code, not the entire thing. (:

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
1
Best Answer

Answer by clunk47 · Dec 04, 2013 at 07:02 PM

 #pragma strict
 
 private var timeLeft : int = 10;
 private var counting : boolean = false;
 
 function Start()
 {
     StartCoroutine(CountDown());
 }
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.F))
         counting = !counting;
     if(timeLeft <= 0 && light && light.enabled)
         light.enabled = false;
 }
 
 function CountDown()
 {
     while(true)
     {
         if(counting && timeLeft > 0)
             timeLeft--;
         yield WaitForSeconds(1);
     }
 }
 
 //FOR TESTING PURPOSES
 function OnGUI()
 {
     GUILayout.Label("Time Remaining - " + timeLeft.ToString() + "\n" + "Press 'F' Key to toggle timer.");
 }
Comment
Add comment · Show 10 · 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 clunk47 · Dec 04, 2013 at 08:25 PM 1
Share

You have time always equal to 30 in update, you are asking if(timeLeft = 0) ins$$anonymous$$d of if(timeLeft == 0) or timeLeft

avatar image clunk47 · Dec 05, 2013 at 02:23 AM 1
Share
 #pragma strict
 
 var matchText : GUIText;
 var match : int = 0;
 var meshRen : $$anonymous$$eshRenderer;
 var otherGO : GameObject;
 var matchRend : $$anonymous$$eshRenderer;
 var otherGameObject : GameObject;
 var ray : Ray;
 var hit : RaycastHit;
 private var timeLeft : int = 30;
 private var counting : boolean = false;
 function Awake () 
 {
     meshRen = otherGO.GetComponent($$anonymous$$eshRenderer);
     matchRend = otherGameObject.GetComponent($$anonymous$$eshRenderer);
     matchRend.enabled = false;
     matchText.text = "";
 }
 
 function Update () 
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {       
         if (Physics.Raycast(ray, hit, 100))
         { // 1000 or $$anonymous$$athf.Infinity should be enough !
             // what did the raycast hit ?
             Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
             Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
             if(hit.collider.gameObject.tag == "match")
             {       
                 meshRen.enabled = false;
                 matchRend.enabled = true;
                 Destroy(hit.collider.gameObject, 3);
                 match += 15;
                 matchText.text = "Aquired 15 matches.";
                 Destroy(matchText, 3);
             }
         }
     }
     
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
     {
         counting = !counting;
         timeLeft = 30;
     }
     
     if(timeLeft <= 0 && light && light.enabled)
         light.enabled = false;
     
 }
 
 function Start()
 {
     StartCoroutine(CountDown());
 }
 
 function CountDown()
 {
     while(true)
     {
         if(match >= 0)
         {
             if(counting && timeLeft > 0)
             {
                 timeLeft--;
                 yield WaitForSeconds(1);
             }
         }
         yield WaitForEndOfFrame();
     }
 }
 
 //FOR TESTING PURPOSES
 function OnGUI()
 {
     GUILayout.Label("Time Remaining - " + timeLeft.ToString() + "\n" + "Press 'F' $$anonymous$$ey to toggle timer.");
 }
 
 
 //Not sure where you're going to be calling this.
 function $$anonymous$$atchCounter()
 {                    
     if(match >= 1)
     {
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
         {
             if(!light.enabled)
             {
                 light.enabled = true;
                 match--;
             }
             else light.enabled = false;
         }
     }
 }
avatar image clunk47 · Dec 05, 2013 at 02:29 AM 1
Share

I have this working without using your other script, so you may want to remove

  if(timeLeft <= 0 && light && light.enabled)
        light.enabled = false;

From Update().

Thanks for re$$anonymous$$ding me, I need to change the link on my profile to my new page, $$anonymous$$anifest was an old project XD

avatar image WaterMinecraft · Dec 05, 2013 at 02:46 AM 1
Share
  if(timeLeft <= 0 && light && light.enabled)
     {
        light.enabled = false;
        counting = !counting;
        timeLeft = 30;
     }

I just added that last bit of code and it works like a charm!! Thank you so much!! :D

avatar image clunk47 · Dec 05, 2013 at 02:48 AM 1
Share

Very cool, glad I was able to help you resolve your concern! Happy Developing :D

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.

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

Inspector doesn't want to show keys in Scripts 2 Answers

Highlight one cell on a GridObject (prefab included) 2 Answers

Use the same input key 1 Answer

Key Press to Trigger animation when within range 1 Answer

How to move an object with a key input? 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