- Home /
How to get FixedUpdate (fixed frame) count since startup?
I am trying to implement a delay system that is based on fixed frames.
There doesn't seem to be a "fixed frame" equivalent for Time.frameCount
...
How can I get the number of fixed updates since the start of the game?
Is Time.fixedTime / Time.fixedDeltaTime
a viable solution? (I think it could fail in some [rare?] situations?)
Answer by LK84 · Aug 05, 2016 at 12:48 PM
Can't you just create your own counter? Something like this:
int frameCounter;
void Start()
{
frameCounter=0;
}
void FixedUpdate()
{
frameCounter++;
}
I need a consistent counter across multiple objects, so unless we are talking about a singleton, that wouldn't work.
I am also trying to avoid a singleton if possible, to avoid unnecessary(?) reference-spagetti that may happen in some cases in this particular project, but I can work around it, now that you meantion it...
Answer by Bunny83 · Aug 05, 2016 at 08:25 PM
To get the fixed frame count you just have to divide Time.fixedTime by Time.fixedDeltaTime as you have discovered. However since those are floating point numbers the result might be slightly off. You can use Mathf.RoundToInt to get an actual integer value.
public static int FixedFrameCount()
{
return Mathf.RoundToInt(Time.fixedTime / Time.fixedDeltaTime);
}
This might get inaccurate after a longer playtime due to the dynamic accuracy range of floating point numbers but should be ok for quite a long period of time. So if you need an more "reliable" count you might want to count manually as the others have shown.
Answer by CamoLeopard · Aug 05, 2016 at 12:49 PM
Have you considered creating a class called Delay or something similar, looking something like this
float fixedFrames = 0;
public static float getFixedFrames(){
return fixedFrames;
}
void FixedUpdate () {
fixedFrames++;
}
I see no problem with doing something like that, you could even slot that into any class and that sould work.
Answer by Norfinity · Aug 05, 2016 at 12:04 PM
Hi, do you mean something like Time.timeSinceLevelLoad ? Otherwise you could increase some float variable: timeSinceStart += Time.fixedDeltaTime;
in your FixedUpdate() function. If you wanna calculate the time since startup through scenes you could use a static float variable.
Your answer
Follow this Question
Related Questions
Time Delay Animation 1 Answer
How do I tell another script to skip its routine for one frame? 1 Answer
Update() calls start several seconds after launch 0 Answers
why does Coroutine 'yield return 0' sometimes take a lot of frames before it continues 1 Answer
FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer