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 JayFitz91 · Jul 27, 2014 at 07:09 PM · updatetimer

Problem with timer in Update function

I have a script which assigns a random number at the beginning of the scene, I have a few if statements to assign a value in seconds to the timer depending on what the number is. Here is some code:

 using UnityEngine;
 using System.Collections;
 
 public class numberOfTaps : MonoBehaviour 
 {
     private float timer;
     private bool ranOnce = false;
 
     private int tapCounter = 0;
     private int number = 0;
 
     void Start()
     {
             //Generate a random number
         number = Random.Range (50, 250);
     }
 
     void Update () 
     {
             
         randomTapping();
 
     }
 
     void randomTapping()
     {
 
         Debug.Log (number);
 
         if(Input.GetButtonDown ("Fire1"))
         {
             tapCounter += 1;
         }

             //Checks to see if the tap count matches the number and turns the object red
         if(tapCounter == number)
         {
             renderer.material.color = Color.red;
         }
 
         //Debug.Log (tapCounter);


         //Sets the timer according to the number given
         if(number >= 50 && number <= 75)
         {
             timer = 10.0f;
         }
         
         else if(number >= 76 && number <= 125)
         {
             timer = 20.0f;
         }
         
         else if(number >= 126 && number <= 175)
         {
             timer = 30.0f;
         }
         
         else if(number >= 176 && number <= 250)
         {
             timer = 45.0f;
         }
         
         //Count down the timer in seconds
         timer -= Time.deltaTime;
         Debug.Log(timer);

         //Destroy the object if the timer reaches zero
         if(timer <= 0)
         {
             Destroy(gameObject);
         }
 
     }

 //Print out relevant information on screen
     void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 100, 20), "Number of taps: " + tapCounter);
         GUI.Label (new Rect (10, 30, 100, 20), "RandomNumber: " + number);
         GUI.Label(new Rect(10, 50, 100, 20), "Time left: " + timer);
 
     }
 
 }

This is in the update function, i can get my random number alright but the timer never counts down as it keeps restarting, is there a way i can get timer to count to zero before it reaches the end of the code block?

Comment
Add comment · Show 3
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 Exbow · Jul 27, 2014 at 07:20 PM 0
Share

"can i get timer to count to zero before it reaches the end of the code block?" you can do that with a do while loop, but this will not be a timer countdown anymore. could you provide more code so we can help accordind to your actual problem?

avatar image JayFitz91 · Jul 27, 2014 at 07:24 PM 0
Share

Sure, i'll update my question with all my code Edit: Code has been updated, problem is the timer never goes below 44 and then loops back to 45 when the code is executed again

avatar image Exbow · Jul 27, 2014 at 07:49 PM 0
Share

i updated my answer. hope that helps!

1 Reply

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

Answer by Exbow · Jul 27, 2014 at 07:24 PM

 //Sets the timer according to the number given
         if(number >= 50 && number <= 75)
         {
             timer = 10.0f;
         }
  
         else if(number >= 76 && number <= 125)
         {
             timer = 20.0f;
         }
  
         else if(number >= 126 && number <= 175)
         {
             timer = 30.0f;
         }
  
         else if(number >= 176 && number <= 250)
         {
             timer = 45.0f;
         }

Why dont you just reposition this to the start function so you set the timer once then?

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 JayFitz91 · Jul 27, 2014 at 07:47 PM 0
Share

Well the random number will be how many times the user will need to click the screen. The timer will be given based on what number comes up as obviously the more clicks needed, the more time that will be needed

avatar image Exbow · Jul 27, 2014 at 07:53 PM 0
Share

well since you need to asign the timer once. place this part of the script after the number = Random.Range (50, 250); on the start function and it will stop reassigning the timer.

avatar image JayFitz91 · Jul 27, 2014 at 07:56 PM 0
Share

Thank you so much, i never even thought of that(trying to over-complicate it ha) Thanks again

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

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

Related Questions

Calling a function only once in Update 6 Answers

convert timer from update() to coroutine or invokerepeating 0 Answers

Using Update loop vs Coroutine for exact timer 3 Answers

For loop resetting itself, but needs to stop 2 Answers

Is an object with an update function possible? (C#) 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