Reset Timer after objects 've been destroyed
Hi guys, im making a Break-out like game and i struggle with a problem:
Besides the normal Ball i have a bigger ball that spawns after x-sec. I can split this ball into two normal size balls. Both normal sized balls shall destroy when colliding with the top of the Map. Now i want the timer to be reseted if both have been destroyed, despite the order which smaller ball gets destroyed last.
Comment
Answer by saud_ahmed020 · Jun 10, 2016 at 06:40 AM
@Ray116GA You can set timer at any time during the gameplay by calling setCountDownTime().
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public delegate void TimeOut();
public static event TimeOut OnTimeOut;
private static Timer _sharedInstance;
private string textTime;
private int minutes;
private int seconds;
private Text textField;
private float countDownTime;
void Awake()
{
_sharedInstance = this;
setCountDownTime();
textField = GetComponent<Text>();
updateTimerLable();
}
void Update()
{
if(GameController.isGamePause)
return;
if (countDownTime < 0)
{
if (OnTimeOut != null)
OnTimeOut();
}
else
{
countDownTime -= Time.deltaTime;
updateTimerLable();
}
}
void OnDestroy()
{
OnTimeOut = null;
}
public static Timer SharedInstance()
{
return _sharedInstance;
}
void updateTimerLable()
{
minutes = Mathf.FloorToInt(countDownTime / 60F);
seconds = Mathf.FloorToInt(countDownTime % 60);
textTime = string.Format ("{0:00}:{1:00}", minutes, seconds);
textField.text = textTime;
}
public void setCountDownTime()
{
countDownTime = GameController.totalTime;
}
public float CountDownTime
{
get{
return countDownTime;
}
set{
countDownTime = value;
}
}
public float Minutes
{
get{
return minutes;
}
}
public float Seconds
{
get{
return seconds;
}
}
}
Your answer
