Search Unity

Time.frameCount since scene load?

Discussion in 'Scripting' started by Milionario, Sep 25, 2020.

  1. Milionario

    Milionario

    Joined:
    Feb 21, 2014
    Posts:
    138
    Time.frameCount counts the total number of frames since the game has started.

    Is there a built in way of knowing the frameCount since the scene was loaded, meaning that when I reload the same scene that value should start at 0.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Just keep track yourself.
    Code (csharp):
    1. private int counter = 0;
    2. void Awake() {
    3. counter = 0;
    4. }
    5. void Update() {
    6. counter++;
    7. }
     
    Bunny83 and PraetorBlue like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Alternatively, if you don't need it every frame, just store it once at scene load and do the math when you need it:

    Code (CSharp):
    1. int frameCountAtSceneStart = 0;
    2.  
    3. void Start() {
    4.   frameCountAtSceneStart = Time.frameCount;
    5. }
    6.  
    7. int framesSinceSceneLoad => Time.frameCount - frameCountAtSceneStart;
     
    Baloouis and Bunny83 like this.