Countdown variable modified with deltaTime continuously being reset

Hi all,

I’m having an issue with deltaTime. I’m creating a script that limits a player’s cursor speed so that there’s a .2 second delay between when the player can move the cursor again, however delta time never seems to reduce my countdown. My outputs of player1MoveDelay and player2MoveDelay when I execute print for each number are constantly somewhere around .82 - .83 for the number itself. What can I do to fix this?

I listed the code below, stripped of everything but the relevant lines.

Thank you.

 using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class SelectControl : MonoBehaviour {
    
    	public float MAX_VALUE = 0.2f;
    	public float player1MoveDelay = 0.2f;
    	public float player2MoveDelay = 0.2f;
    
    	// Use this for initialization
    	void Start () {
    		Time.timeScale = 1;
    	}
    
    
    	void checkMotionControls ()
    	{
    		//Player 1 controls
    		if (player1MoveDelay <= 0.0f) {
    				player1MoveDelay = MAX_VALUE; //then proceed
    		}
    
    		//Player 2 controls
    		if (player2MoveDelay <= 0.0f) {
    				player2MoveDelay = MAX_VALUE; //then proceed

    		}
    	}
    
    	// Update is called once per frame
    	void Update () {
    		checkMotionControls ();
    		checkTime (player1MoveDelay);
    		checkTime (player2MoveDelay);
    	}
    
    	void checkTime (float num)
    	{
    		if (num > 0) {
    			num -= (Time.deltaTime);
                            //for debugging purposes
    			print ("Time " + Time.deltaTime);
    			print ("Number " + num);
    		}
    	}
    }

Here is an example of the output I will get:

63166-deltatime-output.png

player1MoveDelay and player2MoveDelay never actually change. You’re passing them into checkTime() which copies the value and stores it in the local variable “num”.

You can pass by reference instead, I think that’s what you were trying to do:

             ...
             checkTime (ref player1MoveDelay);
             checkTime (ref player2MoveDelay);
         }
     
         void checkTime (ref float num)
         {
             ...