Search Unity

End my timer from another script

Discussion in 'Scripting' started by InMindStudios, Jan 24, 2021.

  1. InMindStudios

    InMindStudios

    Joined:
    Dec 5, 2020
    Posts:
    10
    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:

    Code (CSharp):
    1. public class TimerController : MonoBehaviour
    2. {
    3.     public static TimerController instance;
    4.  
    5.     public Text timeCounter;
    6.  
    7.     private TimeSpan timePlaying;
    8.     private bool timerGoing;
    9.  
    10.     private float elapsedTime;
    11.  
    12.     private void Awake()
    13.     {
    14.         instance = this;
    15.     }
    16.  
    17.     private void Start()
    18.     {
    19.         timeCounter.text = "Time: 00:00.00";
    20.         timerGoing = false;
    21.         TimerController.instance.BeginTimer();                  [B]<-This is where I cheated![/B]
    22.     }
    23.  
    24.     public void BeginTimer()
    25.     {
    26.         timerGoing = true;
    27.         elapsedTime = 0f;
    28.  
    29.         StartCoroutine(UpdateTimer());
    30.     }
    31.  
    32.     public void EndTimer()
    33.     {
    34.         timerGoing = false;
    35.     }
    36.  
    37.     private IEnumerator UpdateTimer()
    38.     {
    39.         while (timerGoing)
    40.         {
    41.             elapsedTime += Time.deltaTime;
    42.             timePlaying = TimeSpan.FromSeconds(elapsedTime);
    43.             string timePlayingStr = "Time: " + timePlaying.ToString("mm':'ss'.'ff");
    44.             timeCounter.text = timePlayingStr;
    45.  
    46.             yield return null;
    47.         }
    48.     }



    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!
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    so i see no "cheat" here :D

    you use the Timer with BeginTimer(); and stop it with EndTimer();

    also i would depending on the needs stop the coroutine which is not done here,

    Code (CSharp):
    1. //To start the timer
    2. TimerController.instance.StartTimer();
    3.  
    4. //To stop the timer
    5. TimerController.instance.StopTimer();
    thats how you call the function from other scripts
     
    InMindStudios likes this.
  3. InMindStudios

    InMindStudios

    Joined:
    Dec 5, 2020
    Posts:
    10
    The cheat is that I put the TimerController.instance.StartTimer(); in the actual same script as the timer is. That way I can start it when I activate that script. That is not what I want though. I want to start it from the EndGame script.
    But I cant get that line of code in my EndGame script to call that function in the TimerController script. :/
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646

    Code (CSharp):
    1. //To start the timer
    2. TimerController.instance.StartTimer();
    to start the timer from any script
     
    InMindStudios likes this.
  5. InMindStudios

    InMindStudios

    Joined:
    Dec 5, 2020
    Posts:
    10
    Ok, my mistake was that I did not put a Void Update() because I got it working on a trigger this morning. But now it works. you were right and I was an idiot. The problem with us newbies is that we seldome have the surrounding script set up right which makes it very frustrating when things just is supposed to work.

    This was the C# Script I used for all you fellow newbies out there: