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 Rush3fan · Jan 19, 2012 at 08:12 AM · not workingoperator

What's wrong with the % operator? Simple question.

I'm trying to get my code to execute at certain frequencies.

So it's rather simple:

time++;

if(time%40==0) cycle2++;

In my other game engine, this executes my code every 40 frames, but It's kind of random in Unity. What do I have to do in Unity to make this work?

Comment
Add comment · Show 1
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 aldonaletto · Jan 19, 2012 at 09:41 AM 0
Share

This will not work if time is a float. If so, declare it as int, or compare to a small value ins$$anonymous$$d of 0:

  if (time%40 < 0.001) cycle2++;

Comparing floats is tricky: both must be exactly the same to produce an equality, and this rarely occurs.

3 Replies

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

Answer by haroonalam · Jan 19, 2012 at 08:18 AM

check the Time properties in unity docx. This is what I had done to trigger function on specific time:

float m_time = 0.0f;

 void Update()
 {
     m_time += 0.01f;

     if (m_time >= CertainFrequency)
        { //do something..
           m_time = 0.0f; }
 }
Comment
Add comment · Show 2 · 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 Rush3fan · Jan 19, 2012 at 08:25 AM 0
Share

Hmm.. that's unfortunate I can't just use an operator. Oh well, I suppose it wouldn't hurt to add some more ti$$anonymous$$g variables. :/

avatar image haroonalam · Jan 19, 2012 at 08:31 AM 0
Share

it seems strange, I am using % its perfectly working, make sure you are importing libraries

avatar image
0

Answer by Kryptos · Jan 19, 2012 at 08:50 AM

Update is called whenever possible. This means that it is not called at a fixed rate.

If you want to call something every n frames, you can use Time.frameCount:

 int n = 40;
 void Update()
 {
     if( (Time.frameCount % n) == 0 )
     {
         // do something
     }
 }

But you can also use a floating value to call something every n seconds:

 int fSeconds = 5.1f;
 int fElapsedTime = 0.0f;
 void Update()
 {
     if( Mathf.Repeat( fElapsedTime, fSeconds ) == 0 )
     {
         fElapsedTime += Time.deltaTime;
         // do something
     }
 }
Comment
Add comment · Show 2 · 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 Rush3fan · Jan 19, 2012 at 09:20 AM 0
Share

Thanks.. I'm trying out those functions now.

avatar image Rush3fan · Jan 19, 2012 at 09:23 AM 0
Share

This is what I'm looking at in particular if this makes any sense.. //Spot positions if((timeAll+(0*sFrequency))%(4*sFrequency)==0) {vLine1=v$$anonymous$$irror;cycle1++;} if((timeAll+(1*sFrequency))%(4*sFrequency)==0) {vLine2=v$$anonymous$$irror;cycle1++;} if((timeAll+(2*sFrequency))%(4*sFrequency)==0) {vLine3=v$$anonymous$$irror;cycle1++;} if((timeAll+(3*sFrequency))%(4*sFrequency)==0) {vLine4=v$$anonymous$$irror;cycle1++;}

avatar image
0

Answer by Rush3fan · Jan 19, 2012 at 09:24 AM

Sorry that didn't format well..

    //Spot positions
     if((timeAll+(0*sFrequency))%(4*sFrequency)==0)
     {vLine1=vMirror;cycle1++;}
     if((timeAll+(1*sFrequency))%(4*sFrequency)==0)
     {vLine2=vMirror;cycle1++;}
     if((timeAll+(2*sFrequency))%(4*sFrequency)==0)
     {vLine3=vMirror;cycle1++;}
     if((timeAll+(3*sFrequency))%(4*sFrequency)==0)
     {vLine4=vMirror;cycle1++;}
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script Efficiency 1 Answer

Array Problem - Error Code BCE0022 2 Answers

Official Unity Space Shooter Tutorial 1 Answer

My AI keeps flickering between stopping and moving. 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