Hey guys. Total newbie here.
I am bulding a VR Game. In this game I have a timer that starts when game begins. I got this timer from a guide on youtube and it works but I have no idea how to call its functions from another script so I cheated to get it to start. But now I am screwed since I need it to end as well.
The Script looks like this:
public class TimerController : MonoBehaviour
{
public static TimerController instance;
public Text timeCounter;
private TimeSpan timePlaying;
private bool timerGoing;
private float elapsedTime;
private void Awake()
{
instance = this;
}
private void Start()
{
timeCounter.text = "Time: 00:00.00";
timerGoing = false;
TimerController.instance.BeginTimer(); [B]<-This is where I cheated![/B]
}
public void BeginTimer()
{
timerGoing = true;
elapsedTime = 0f;
StartCoroutine(UpdateTimer());
}
public void EndTimer()
{
timerGoing = false;
}
private IEnumerator UpdateTimer()
{
while (timerGoing)
{
elapsedTime += Time.deltaTime;
timePlaying = TimeSpan.FromSeconds(elapsedTime);
string timePlayingStr = "Time: " + timePlaying.ToString("mm':'ss'.'ff");
timeCounter.text = timePlayingStr;
yield return null;
}
}
The TimerController.instance.BeginTimer(); is the command that seems to start the timer. And the same with EndTimer. But I have no idea how to call this function in this script “Timercontroller” from my “EndGame” script.
It seems to have something to do with the public void but I have now googled, youtubed and read for three days and cannot find a solution that I understand.
I also want the end time to display when I destroy the last object. That is my next mission.
Thank you all in advance!