I can find numerous tutorials online about how to make a simple countdown timer in Unity, but none about how to subtract time from it if you press a button.
I am making a 2-D platformer game where if your character passes in front of an NPC, it brings up dialog. I want to make so that one option of dialog does nothing to the timer, but the other option will subtract 10 seconds from the timer. How do I do this?
private float timer;
private Button targetButton;
void Awake ()
{
timer = 50f;
// this line below only in case if you don't assign the SubtractTime method
// to button from inspector otherwise comment this line
targetButton.onClick.AddListener( () => SubtractTime() );
}
void Update ()
{
timer -= Time.deltaTime;
}
// Add this function to a button's onClick event
public void SubtractTime ()
{
timer -= 10f;
}