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 smirlianos · Jun 20, 2014 at 05:59 PM · triggertimetimerfloatcountdown

Start timer with trigger

Hello,

I need to start a timmer when a boolean is true, but I can't make it work. Any help?

Thanks in advance!

 #pragma strict
 var burn : boolean;
 var col : Color = Color.white;
 private var timer : float;
 
 function Update() {
     timer = Time.deltaTime;
     if(burn)
     {
         ResetTimer();
         col = Color.Lerp(Color.white, Color.red, timer);
     }
     renderer.material.color = col;
 }
 
 function ResetTimer() {
     timer = 0.0;
 }
Comment
Add comment · Show 8
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 Landern · Jun 20, 2014 at 06:03 PM 0
Share

Can't make it work? Post your script and lets see if we can help.

avatar image smirlianos · Jun 20, 2014 at 06:05 PM 0
Share

Topic edited

avatar image fafase · Jun 20, 2014 at 06:31 PM 0
Share

You have 750+ of karma but is asking something really trivial and propose a script that makes little sense. Am I missing something here?

burn is not set anywhere, ResetTimer does not have any purpose apart from setting timer to 0 which will stop any lerping from happening. As a result, when getting inside the if statement, timer is 0, lerp is not doing.

I guess you meant to use

 timer += Time.deltaTime;
avatar image fafase · Jun 20, 2014 at 06:41 PM 1
Share

Ok so burn comes from somewhere else but let's see your Update:

 function Update() {
     timer = Time.deltaTime; // Timer is set to deltaTime 
     if(burn)    // if true
     {
         ResetTimer();  // Timer is set to 0

         // Nothing happens since the ratio is 0
         col = Color.Lerp(Color.white, Color.red, timer); 
     }
     renderer.material.color = col; // Color does not change
 }

Now you could reset timer after the lerp so that the value is applied to the Lerp. But actually the timer = deltaTime, is useless where it is if not use somewhere else.

I just realize as well that your color may not change since you are lerping from white to red with the same ratio.

Here is a review: var timer :float = 0; function Update() { if(burn)
{ timer += Time.deltaTime; renderer.material.color = Color.Lerp( renderer.material.color, Color.red, timer); } }

This code above will lerp from current color to red over 1 second, current probably being white from the start.

avatar image fafase · Jun 20, 2014 at 08:48 PM 1
Share

Add a multiplier to the timer

 timer += Time.deltaTime * 1 / speed;


Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dreamora · Jun 23, 2014 at 08:55 AM

The problem with your code is that you use if(burn) only. If you do not want to restart the timer every frame again (which basing on the code could happen), you would need to have an

 if (burn && timer <= 0.0)

To ensure that float inaccuracy does not hit you, I would set the timer to -1 when its reset to clearly mark it as 'not running', but you could instead use a second bool to track the timer being 'running' independent of using its timer value

Comment
Add comment · 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

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

Time does not start counting down when need to 1 Answer

CountDown Timer Help (Seconds problem) 2 Answers

How to stop a Countdown Timer? 1 Answer

Format String Minutes:Seconds 3 Answers

Stop and Resume Timer 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