Issue with Time.DeltaTime - Causing random numbers?

Hello,

I’m working on a project at the moment that requires a counter to increment once per second until it reaches a variable I can define later. In my head it makes sense however when I run the code I am getting strange results. I’ve read online that the timescale settings can be off so I’ve attached what they are, as well as the code and the outputs I’m getting.

I hope this all helps :slight_smile:

if (ls_v && rs_h)
        {
            //outcome = "Correct";
            float counter = 0f;
            counter += Time.deltaTime;

            Debug.Log(counter);

            if (counter == 3f)
            {
                outcome = "Correct";
                isPreaching = false;
                Complete();
            }

        }

134022-capture.png

Hello. I found this two years too late, but I figured I would answer it in case someone else stumbles across it via Google.

Your “float count = 0;” should not be in the loop. Every time your loop occurs, it sets “count” back to zero.
It should look something like this:

private float counter = 0f;

//...some code

Update(){

    //...some code
    
    if (ls_v && rs_h)
             {
                 //outcome = "Correct";
                 counter += Time.deltaTime;
     
                 Debug.Log(counter);
     
                 if (counter == 3f)
                 {
                     outcome = "Correct";
                     isPreaching = false;
                     Complete();
                 }
     
             }

        //...some more code
}//end of Update()