Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Stopping a timer on trigger

Discussion in 'Scripting' started by MIST0, Jul 16, 2015.

  1. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    I have a timer which counts up when the game starts. I need to stop the timer when the car goes over the finished point.

    The timer code is attached to a Text object.

    Code (CSharp):
    1. public bool isRacing;
    2.     Text txt;
    3.    
    4.     void Awake()
    5.     {
    6.         txt = GetComponent<Text>();
    7.     }
    8.    
    9.     void Start()
    10.     {
    11.         isRacing = true;
    12.     }
    13.    
    14.     void Update ()
    15.     {
    16.         if(isRacing)
    17.             txt.text = Time.time.ToString("#.00");
    18.     }
    How would I go about getting the timer to stop when the timer hits a trigger/finished line.
     
  2. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    You need to attach to your trigger finish line a script like that: (and obviously assign your car in the inspector)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class YourScriptName : MonoBehaviour {
    6. public GameObject Car;
    7.  
    8.     void OnTriggerEnter(Collider coll)
    9.     {
    10. Car.GetComponent<YourCarScriptName>().isRacing = false;
    11.       }