Search Unity

Question Stopwatch on trigger enter

Discussion in 'Scripting' started by daX-_-_908, Jun 20, 2020.

  1. daX-_-_908

    daX-_-_908

    Joined:
    Mar 26, 2020
    Posts:
    3
    Hi, I'm new to Unity and I'm still learnig.
    I made a little level where a ball has to reach the end before the timer reach the 0.
    When the player reach the end a win screen appear and show you the score you have made and your time to complete the level.
    What I want to know is: How can I stop the time in the winscreen after the player reached the end?

    If they can be usefull down here there are the stopwatch script and the end trigger script:
    Stopwatch:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using TMPro;
    5. using UnityEngine;
    6.  
    7. public class stopwatch : MonoBehaviour
    8. {
    9.     public float timeStart = 0;
    10.     public TextMeshProUGUI textBox;
    11.     private bool isGameStarted = false;
    12.     private bool gameEnded = false;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         textBox.text = timeStart.ToString();
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.        
    26.             timeStart += Time.deltaTime;
    27.             Debug.Log(timeStart);
    28.        
    29.         textBox.text = Mathf.Round(timeStart).ToString();
    30.  
    31.         if (timeStart < 0)
    32.         {
    33.             timeStart = 0;
    34.             isGameStarted = false;
    35.             gameEnded = true;
    36.  
    37.         }
    38.  
    39.         if (gameEnded == true)
    40.         {
    41.             FindObjectOfType<Gamemanager>().EndGame();
    42.            
    43.  
    44.         }
    45.     }
    46. }
    Endtrigger:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Endtrigger : MonoBehaviour
    6. {
    7.     public  Gamemanager Gamemanager;
    8.     public GameObject Scermatadicountdown;
    9.  
    10.  
    11.  
    12.  
    13.  
    14.     private void OnTriggerEnter(Collider other)
    15.     {
    16.         Scermatadicountdown.GetComponent<countdown>().enabled = false;
    17.         Gamemanager.Completelevel();
    18.     }
    19. }