Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 scopique · Apr 18, 2010 at 12:38 AM · timermodulus

Timer For Occasional Action

I've done some searching, and have found a few examples that KIND OF touch on what I'm looking to do, but I'm getting some odd behavior.

I'm looking to create a timer that will execute a block of code every, say, 5 minutes. My thought was to use "VALUE % INTERVAL" and if the value was 0, execute the code.

In a short test, however, I'm getting something...unusual (C#):

void Update()
{
    float count = Mathf.Floor(Time.time % 10.0f);
    if (count == 0)
    {
         print("OK!");
    }
}

I figure that in this case, a simple Time.time mod 10 would eventually tick over to 0, then would start again as Time.time continued on it's way. The Mathf.Floor() is there because otherwise, a modulus operation could return 0.1, 0.2, 0.5, etc, when all I want is 0.

However, when simply debugging the Mathf.Floor(Time.time % 10.0f), the output window counts to 9...and then stops. It doesn't flip over or anything.

I've looked at other timer methods mentioned around here, but none seem to work for what I'm looking to do. I noticed one, written in JS, but when I attempted to translate to C# for my purposes, didn't fit the bill.

Any suggestions?

Thanks! Chris

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

Answer by Peter G · Apr 18, 2010 at 01:53 AM

My suggestion would be to use Invoke or InvokeRepeating and set the time to however long you want.

void Start () { InvokeRepeating("SayHello", 0, 10); }

void SayHello () { print("Ok"); }

Comment
Add comment · Show 1 · 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 scopique · Apr 18, 2010 at 04:33 PM 0
Share

Oh man. Thanks for the heads up. :D I'll give this a shot.

avatar image
1

Answer by Cyclops · Apr 18, 2010 at 02:06 AM

Peter's solution is best, InvokeRepeating will do exactly what you want. But to address your counting code not work (since I already ran a test :) , I suspect it's a typo in your code. I did this:

public float count;
void Update () {
    count = Mathf.Floor(Time.time % 10.0f);
}

And it worked as expected. Looking at it in the Inspector, count went from 1,2,3...9,0,1,2,3...

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 scopique · Apr 18, 2010 at 04:37 PM 0
Share

Hmmm. You're right. I was using print(count); to check the values in the console, but in the Inspector, it rolls over normally.

I'm going to chalk it up to weirdness, and give Peter's answer a go.

Thanks!

avatar image Ony · Jun 09, 2010 at 07:27 AM 1
Share

Something similar happened to me and it turned out that I just needed to uncheck "collapse" in the Unity console window. When collapse is on it causes repeated print statements not to be shown.

avatar image Cyclops · Jun 09, 2010 at 02:00 PM 0
Share

@Jak, yeah, that could be it. I do remember some other surprises from collapse.

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

No one has followed this question yet.

Related Questions

GUI elements vanish when publishing 1 Answer

Cannot Get Light To Flash - Help 1 Answer

Countdown Timer Help About putting 0's 1 Answer

Timer counts down unwanted 2 Answers

Timer record 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