- Home /
The timer starts after a collision with an object
Hello all!
I have a Timer script (C#):
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Timer : MonoBehaviour {
 
     public Text counterText;
     public bool TimerOn;
 
     public float seconds, minutes;
 
     void Start(){
         TimerOn = false;
         Text counterText = GetComponent<Text>();
         counterText = GetComponent<Text>() as Text;
     }
 
     void Update(){
         if (TimerOn = true){
             minutes = (int)(Time.time / 60f);
             seconds = (int)(Time.time % 60f);
             counterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
         }
     }
 }
And I want to do when my car will collide with the invisible object, for example, (Box1) is a timer starts running but if a collision with an object (Box2) is the timer will stop working.
Someone would like to help me? It was not until I begin programming with Unity so please bear with us.
*I am using google translator because I come from Polish, and I do not speak much English. :)
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by losingisfun · Jan 03, 2017 at 12:36 PM
You should look up the API for OnTriggerEnter and CompareTag
 void OnTriggerEnter (Collider other) {
      if (other.gameObject.CompareTag("Box1")) {
           TimerOn = true;
      } else if (other.gameObject.CompareTag("Box2")) {
           TimerOn = false;
      }
 }
Create a tag called "Box1" and "Box2" and put it on the two GameObjects that you want to collide with (make sure they have colliders as well)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                