Timer not working properly

I’ve done a timer script so when in battle once player has attacked the enemy it’s suppose to count down the timer from 10 and when ) is hit, it lets my player attack again (ie, attack menu pops back up) very much like FF10 / FF10-X2

Script attached below if anyone can figure out whats happening with it, the menu doesn’t pop backup and the timer only goes to 9 and nothing else.

var timer : int = 10;
function Update () {
        if(turn == 2 && enemyTurnActivated == false) {
                enemyTurnActivated = true;
                enemyTurn();
               
        }    timer -= Time.deltaTime;
              
       
       
       
}



function enemyTurn () {   player.SendMessage("damagePlayer", Random.Range(1,10));
        if (timer <= 0)
        {          
            
           
        turn = 1;
        enemyTurnActivated = false;
        }    
      
       
}

This is probably a place you would want to call a coroutine to set the timer countdown every second and restore game play. You could look up WaitForSeconds and get some examples in the script reference. It would something like:
while(timer > 0){
yield WaitForSeconds(1);
timer –
}
now do whatever

somethng like this?

function enemyTurn () {   player.SendMessage("damagePlayer", Random.Range(1,10));
        enemyTurnActivated = false;
        yield WaitForSeconds (5);
        turn = 1;
      
        }   
    
      
}

Yeah, but if you use a loop, there’s a countdown:

function enemyTurn () {  player.SendMessage("damagePlayer", Random.Range(1,10));
       enemyTurnActivated = false;
       timer = 5;
      while(timer>0){
       yield WaitForSeconds (1);
      timer--;
        }
        turn = 1;
   
     }
   
  
}
1 Like