Hello, I have a Timer script for my game. In this game, If player kill the enemy, I wan to add 5 seconds to timer.
I have a score variable for enemey healt screen and working. (If kill the enemy change value)
And I want to use this variable and If score value change it, I want to add 5 second in timer.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score; // The player's score.
Text text; // Reference to the Text component.
public float startTime;
private string seconds;
private float addSecond = 5f;
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
// Reset the score.
score = 0;
startTime = Time.time;
}
void Update ()
{
float t = Time.time - startTime ;
string minutes = ((int)t / 60).ToString ();
seconds = (t % 60).ToString ("f2");
text.text = minutes + ":" + seconds;
}
}
I do not know how can ı write code in update function ? In update function, I want to write if score value change it, Add 5 seconds to timer and then reset the score value = 0.
Thank u for answer I change the code I add “bonustTime” variable. But in my opinion I have two logical error;
bonusTime value is 5 not mean 5 second. (Because seconds value = (t % 60) maybe bonusTime = 65)
2)In my opinion I change time value but only one per frame because all code inside the update function.
public class ScoreManager : MonoBehaviour {
public static int score; // The player's score.
Text text; // Reference to the Text component.
public float startTime;
private float bonusTime = 5f;
private float t;
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
// Reset the score.
score = 0;
startTime = Time.time;
}
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
//text.text = "Score: " + score;
if (score != 0) {
t = Time.time - startTime + bonusTime;
score = 0; // reset value for
print("Add Time");
}
else
t = Time.time - startTime;
string minutes = ((int)t / 60).ToString ();
string seconds = (t % 60).ToString ("f2");
text.text = minutes + ":" + seconds;
}
}