Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Timing issue on android device. (Timer/Time.deltaTime; comparing float values)

Discussion in 'Scripting' started by MidnightGameDeveloper, Dec 3, 2019.

  1. MidnightGameDeveloper

    MidnightGameDeveloper

    Joined:
    Apr 26, 2014
    Posts:
    23
    Hello,
    i have a game where I time/measure the reaction time of a player. After that i start a timer/counter which increases by Time.deltaTime and I display it as a string.

    To ensure that the timer doesnt "overshoot" on low fps i wrote this code:

    Code (CSharp):
    1.      
    2. ...
    3. float timeCounter, reactionTime;
    4. ...
    5.  
    6. //This method gets called from Update()
    7. void TimerMethodABC()
    8. {
    9.   //Increase Timer for player
    10.         if (timeCounter < reactionTime && increaseTimer) {
    11.             timeCounter += Time.deltaTime;
    12.             timeCounter = (timeCounter > reactionTime) ? reactionTime : timeCounter; //Limit
    13.             player.DisplayTimer(timeCounter); // display in ui  >> reactionTime.text = timeCounter.ToString("F2");
    14.  
    15.             if(timeCounter >= reactionTime)
    16.                 increaseTimer = false;
    17.         }
    18. }
    19. ...
    20.  
    When i play my game in editor it works as expected, even if lock the fps down to 10.
    As soon as i play it on my android phone it doesnt work as expected. The displayed time does overshoot the reactionTime.

    Example:
    reactionTime from the player => reactionTime = 1.9;
    displayed time in the editor => 1.9
    displayed time in the android build => 1.92 (for example)

    I dont understand how this is actual possible when I set the TimeCounter equal to the reactionTime (if its higher) just one line before displaying it. Is there a better way to compare floats maybe?

    I would appreciate any help.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,090
    Is it possible that the reactionTime is actually 1.92, but you are displaying it in a way that limits the precision to only one decimal place, so that it looks like 1.9?
     
    MidnightGameDeveloper likes this.
  3. MidnightGameDeveloper

    MidnightGameDeveloper

    Joined:
    Apr 26, 2014
    Posts:
    23
    I am using this line of code to display it (same way in editor and game so):
    Code (CSharp):
    1. sometextcomponent.text = timeCounter.ToString("F2");
     
    Last edited: Dec 3, 2019
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,090
    In your OP, reactionTime is a float, but in the post above you appear to be using it as a UI.Text, and you are using it to display the value of the timeCounter variable. I was asking about the reactionTime variable in the OP.
     
  5. MidnightGameDeveloper

    MidnightGameDeveloper

    Joined:
    Apr 26, 2014
    Posts:
    23
    Thanks for your reply.
    Sorry i messed this post up , i edit it. I also use the same function "ToString("F2");" to display the reaction time first.
     
  6. MidnightGameDeveloper

    MidnightGameDeveloper

    Joined:
    Apr 26, 2014
    Posts:
    23
    I was searching at the wrong point, your hint pushed me back onto the right way, thanks!
    It had something to do with when/how i display the reactionTime. I switched to LateUpdate() in my UI-Component and now it works. The reason why it worked in the editor is probably that the script execution order is different between the editor and the build.
     
unityunity