Time.frameCount vs. Time.renderedFrameCount

Hi,

the documentation about the UnityEngine.Time class states

Time.frameCount: The total number of frames that have passed.

Are these render-frames or physics-frames (FixedUpdates)?

Since the documentation just says frames, it might be render frames, BUT: What’s Time.renderedFrameCount then? It’s not in the docs. Comparing frameCount and renderedFrameCount, I saw that frameCount increases slower than renderedFrameCount, which made me think frameCount could be the physics frames (I have physics framerate set to 50, while the render framerate in my test case was usually higher).

Still, trying something like this

int _lastFrame = -1;
protected override void FixedUpdate()
{
	base.FixedUpdate();
	if(_lastFrame == Time.frameCount)
		Debug.LogError("Same frameCount!");
	_lastFrame = Time.frameCount;
}

gives me a lot of “Same frameCount!” in the Console…
Any suggestions?

Tobi

What I’ve found by testing is:

Time.frameCount

  • The number of times the main Unity loop has run.
  • Not affected by Time.timeScale and not related to Physics update.
  • Increments only once per Unity update before any of the main updates are called, so the value can be the same over multiple frames for loops that can run multiple times per update (FixedUpdate, OnGUI).
  • Could be thought of as a count of the number of times that Update has run, though technically it’s incremented before Update is called each frame.


Time.renderedFrameCount

  • The number of frames rendered.
  • Not affected by Time.timeScale and not related to Physics update.
  • Continues to increment when the Play is paused in the editor.
  • Increments at least once per Unity loop at the beginning of the frame.
  • Also increments immediately after LateUpdate right before OnWillRenderObject. (See this for information on order of events.)


Some simple logging code (below) shows that Time.renderedFrameCount will rapidly outpace Time.frameCount, always being Time.frameCount * 2 - 1 (the first update frame it’s only incremented once, but then is incremented twice per frame thereafter) in my tests. This is the case whether or not VSync is enabled.

using UnityEngine;
using System.Collections;

public class RenderTest : MonoBehaviour {

    void Update() {
        Debug.Log("UPDATE: Frame count = " + Time.frameCount + "

Rendered frame count = " + Time.renderedFrameCount);
}

    void FixedUpdate() {
        Debug.Log("FIXED UPDATE: Frame count = " + Time.frameCount + "

Rendered frame count = " + Time.renderedFrameCount);
}

    void LateUpdate() {
        Debug.Log("LATE UPDATE: Frame count = " + Time.frameCount + "

Rendered frame count = " + Time.renderedFrameCount);
}

    void OnWillRenderObject() {
        Debug.Log("OnWillRenderObject: Frame count = " + Time.frameCount + "

Rendered frame count = " + Time.renderedFrameCount);
}

    void OnGUI() {
        Debug.Log("OnGUI: Frame count = " + Time.frameCount + "

Rendered frame count = " + Time.renderedFrameCount);
}
}

Physics update has a separate frame count and can run zero or many times a frame depending on what the physics frame rate is ( and how long your frame actually ran )

@or1on I had the same problem and found the reason for it finally.
See this thread, which quotes the official docs on Execution Order:

… FixedUpdate: FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high…

Hope that’s helpful.