Timer starting with a trigger/collision

Updated on January 3, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on December 29, 2018

SO I’m trying to make a 2D Game where you walk and hop around. Shooting and ammoboxes are also a thing. So I have one problem with the ammobox. I want a timer to start when u activate the collider trigger.

 

I’m using this code atm:

 

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class AmmoChest : MonoBehaviour {
 
public float startTime;
 public Text timerText;
 private float timeLeft = 29F;
 public bool CollidedWithPlayer = false;
 public bool timerRuns = false;
 
// Use this for initialization
 void Start () {
 }
 
void OnCollisionEnter2D(UnityEngine.Collision2D collisionInfo)
 {
 CollidedWithPlayer = true;
 Debug.Log("Hit");
 }
 // Update is called once per frame
 void Update () {
 if (CollidedWithPlayer == true)
 {
 float t = timeLeft -= Time.deltaTime;
 
string minutes = ((int)t / 60).ToString("00");
 string seconds = (t % 60).ToString("00");
 string milliseconds = ((int)(t * 100f) % 100).ToString("00");
 
timerText.text = minutes + ":" + seconds + ":" + milliseconds;
 
if (timeLeft <= 0)
 {
 timeLeft = 0;
 timerText.text = minutes + ":" + seconds;
 }
 }
 else
 {
 Debug.Log("Doesnt work");
 }
 }
}
 

 

So it prints out the doesn’t work debug message in the console, but the timer is still starting as soon as I start the game. Thanks for your help!

 

Klofessor

  • Liked by
Reply
0 on January 1, 2019

Are you checking for the bool timerRuns? It seems that you want to be able to toggle if the timer is counting or not, but you never reference it in the script, and that might be why it starts at the beginning. You might want to try this:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
  
public class AmmoChest : MonoBehaviour {
  
public float startTime;
 public Text timerText;
 private float timeLeft = 29F;
 public bool CollidedWithPlayer = false;
 public bool timerRuns = false;
  
// Use this for initialization
 void Start () {
 }
  
void OnCollisionEnter2D(UnityEngine.Collision2D collisionInfo)
 {
 CollidedWithPlayer = true;
 Debug.Log("Hit");
 }
 // Update is called once per frame
 void Update () {
 if (CollidedWithPlayer == true && timerRuns == true) //You check here if the bool is true. You might want to set it to true at some point in the code.
 {
 float t = timeLeft -= Time.deltaTime;
  
string minutes = ((int)t / 60).ToString("00");
 string seconds = (t % 60).ToString("00");
 string milliseconds = ((int)(t * 100f) % 100).ToString("00");
  
timerText.text = minutes + ":" + seconds + ":" + milliseconds;
  
if (timeLeft <= 0)
 {
 timeLeft = 0;
 timerText.text = minutes + ":" + seconds;
 }
 }
 else
 {
 Debug.Log("Doesnt work");
 }
 }
}
 
It seems that you can just make it if you collide with the player, the timerRuns bool is true, so then it can run.
  • Liked by
Reply
Cancel
0 on January 3, 2019

Thank you for your help!

Idk if its just me, but your code looks really strange with line numbers, indexes, php keywords etc. and /div

  • Liked by
Reply
Cancel
0 on January 3, 2019

So I basically should set timerRuns to true as soon as I hit the player:

void OnCollisionEnter2D(UnityEngine.Collision2D collisionInfo)
 {
 CollidedWithPlayer = true;
  timerRuns = true;
 Debug.Log("Hit");
 }
 
and then check for the timerRuns variable:
 
 void Update () {
 if (timerRuns == true && CollidedWithPlayer == true)
 {
 float t = timeLeft -= Time.deltaTime;
  
string minutes = ((int)t / 60).ToString("00");
 string seconds = (t % 60).ToString("00");
 string milliseconds = ((int)(t * 100f) % 100).ToString("00");
  
timerText.text = minutes + ":" + seconds + ":" + milliseconds;

  • Liked by
Reply
Cancel
0 on January 3, 2019

My code now also looks really strange…

  • Liked by
Reply
Cancel
0 on January 3, 2019

 

Is this better? (It now looks fine to me)

So is this what I needed to change

 void OnCollisionEnter2D(UnityEngine.Collision2D collisionInfo)
 {
 CollidedWithPlayer = true;
 timerRuns = true;
 Debug.Log("Hit");
 }

 

void Update () {
 if (timerRuns == true && CollidedWithPlayer == true)
 {
 float t = timeLeft -= Time.deltaTime;
string minutes = ((int)t / 60).ToString("00");
 string seconds = (t % 60).ToString("00");
 string milliseconds = ((int)(t * 100f) % 100).ToString("00");
timerText.text = minutes + ":" + seconds + ":" + milliseconds;
  • Liked by
Reply
Cancel