- Home /
Question by
richener · Jan 23, 2017 at 12:51 PM ·
c#timer countdownlife
player life lose when timer is run out
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;
}
}
Comment
this is my life manager class
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Life : $$anonymous$$onoBehaviour {
public int playerLife;
private int lifecounter;
public GameObject wrongPOPUP;
public GameObject gameOverScreen;
public GameObject expireScreen;
public Text life;
private Timer leftTime;
void Start(){
life = GetComponent<Text> ();
lifecounter = playerLife;
}
void Update(){
life.text = "" + lifecounter;
if (lifecounter <= 0) {
lifecounter = 0;
gameOverScreen.SetActive (true);
}
}
public void wrongAnswer(){
lifecounter--;
StartCoroutine (wrongLoading ());
wrongPOPUP.SetActive (true);
}
IEnumerator wrongLoading(){
yield return new WaitForSeconds (0.3f);
wrongPOPUP.SetActive (false);
}
}
Best Answer
Answer by juicyz · Jan 24, 2017 at 02:50 AM
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);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Invoke is not working consistently 0 Answers
timer not ticking down 2 Answers