How to start and stop timer when player colide with gameobject?
Hi. I did countdown timer but i can't make to start and stop when collide with gameoject. My countdown script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class myTimer : MonoBehaviour { public float mycolltimer; public Text timerText; public Text scoreText; public bool timerIsActive; public int timescore; //public GameObject starttimer; //public GameObject stopTimer;
// Start is called before the first frame update
void Start()
{
timerText = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
if (timerIsActive == true)
{
mycolltimer -= Time.deltaTime;
//Show second minutes and hiurs
int second = (int)(mycolltimer % 60);
int minutes = (int)(mycolltimer / 60) % 60;
int hours = (int)(mycolltimer / 3600) % 24;
//What time format gonna show in UI
string timeString = string.Format("{0:0}:{1:00}:{2:00}", hours, minutes, second);
timerText.text = timeString;
print(mycolltimer);
//When timer is gone show score.
if(mycolltimer <= 0)
{
timerIsActive = false;
updateScore();
}
}
}
void updateScore()
{
mycolltimer = 10;
mycolltimer += timescore;
scoreText.text = mycolltimer.ToString();
print("score is" + mycolltimer);
}
}
Comment