• 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 Pancar · May 23, 2018 at 10:33 AM · fpscoroutinewaitforsecondsienumeratorwhile-loop

fps calculation with while loop in coroutine

Hi, ım trying to call 24times per 1 second but something is wrong.My test code like this;

 int frame;
 bool stop;

 void Start ()
 {
     StartCoroutine (Test ());
 }

 IEnumerator StopTest ()
 {
     yield return new WaitForSeconds (10f);
     stop = true;
 }

 IEnumerator Test ()
 {
     StartCoroutine (StopTest ());
     while (!stop) {
         frame++;
         yield return new WaitForSeconds (1f / 24f);
     }

     Debug.Log (frame / 24f);
 }

ı should see always "10" on console but my results are like this;

first try 7.583333 second 7.25 7.625...

i dont know what is wrong

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
0

Answer by hexagonius · May 23, 2018 at 05:42 PM

The documentation reveals the answer for your problem:
https://docs.unity3d.com/ScriptReference/WaitForSeconds.html

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

Answer by kyle-misner-kriel · May 23, 2018 at 06:19 PM

Unity can't wait for exactly 1f / 24f of a second. Every frame you'll have slightly different delays which are slightly above (or much more then) 0.0416666667.

Getting your value to reach nearly 10 will require writing some time regulation code, which is less simple.

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 Pancar · May 27, 2018 at 10:10 AM 0
Share

i was try with system.timers, but i'm getting this error;

Internal_GetWidth can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. UnityEngine.Texture2D:GetPixels()

i need to read texture 24 times in one second for video recording $$anonymous$$y last code like this;

 void StartRecord ()
 {
     myTimer.Elapsed += new ElapsedEventHandler (AddFrames);
     myTimer.Interval = 41.6;
     myTimer.Start ();
     StartCoroutine (StopVideoRecording ());
 }

 void AddFrames (object sender, System.EventArgs e)
 {
     Color[] pix_depth = texture.GetPixels ();
     

system.timers it works well but now i have diffrent problem. Do you have any advice?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

yield return waitforseconds not waiting? 3 Answers

wait a second before say path is valid? 2 Answers

Coroutine doesn't work when called from another method. 3 Answers

[ Coroutine: Move Next ] CPU usage 0 Answers

yield return new WaitForSeconds () not working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges