I have life 0f 3hearts and a timer of 60 secs and when the timer is run out my lives will loose to 1 heart
how?
this is my timer class
public class Timer : MonoBehaviour {
public float countdownTimer;
private Text timeText;
private Life lifeSystem;
private
void Start () {
timeText = GetComponent<Text> ();
lifeSystem = FindObjectOfType<Life> ();
}
void Update () {
countdownTimer -= Time.deltaTime;
timeText.text = "" + Mathf.Round (countdownTimer);
if (countdownTimer <= 0) {
countdownTimer = 0;
}
}
juicyz
2
I would do something like this:
public class Life : MonoBehaviour {
public int playerLife;
private int lifecounter;
public GameObject wrongPOPUP;
public GameObject gameOverScreen;
public GameObject expireScreen;
public Text life;
public float countdownTimer;
private Text timeText;
void Start() {
// What was Timer object called before?? replace name.
life = GetComponent<Text> ();
timeText = GameObject.Find("Timer").GetComponent<Text>();
lifecounter = playerLife;
}
void Update() {
countdownTimer -= Time.deltaTime;
timeText.text = "" + Mathf.Round (countdownTimer);
if (countdownTimer <= 0) {
countdownTimer = 0;
lifecounter--;
}
if (lifecounter <= 0) {
lifecounter = 0;
gameOverScreen.SetActive (true);
}
life.text = "" + lifecounter;
}
public void wrongAnswer(){
lifecounter--;
StartCoroutine (wrongLoading ());
wrongPOPUP.SetActive (true);
}
IEnumerator wrongLoading(){
yield return new WaitForSeconds (0.3f);
wrongPOPUP.SetActive (false);
}
}