- Home /
How can I add time to a timer on an object collision?
In my game I have a timer that will countdown from 10, but if the player hits these orbs then I want to have an additional 5 seconds be added to the timer.
Here is my timer code.
using UnityEngine; using System.Collections;
public class Timer : MonoBehaviour {
public float time = 11;
IEnumerator Do() {
yield return new WaitForSeconds(3);
Application.LoadLevel(Application.loadedLevel);
}
// Use this for initialization
void Start () {
}
void Update () {
time -= Time.deltaTime;
}
void OnGUI(){
if (time > 1) {
GUI.Label (new Rect (100, 100, 200, 100), "" + (int)time);
}
else {
GUI.Label (new Rect (100, 100, 200, 100), "Game Over");
StartCoroutine(Do());
}
}
}
Here is my collsion.
using UnityEngine; using System.Collections;
public class Des : MonoBehaviour {
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
}
}
Also I was looking for a way to make the timer not be able to go past 20 seconds. For that i was thinking about something like
if(time >= 20){ time = 20; }
Answer by Qasem2014 · Mar 13, 2015 at 06:26 PM
so add extra time in OnTriggerEnter function ! where is the problem ?
void OnTriggerEnter(Collider other)
{
time+=5; if(time >= 20){ time = 20; } Destroy(other.gameObject,0.1);
}
and your if is a good idea
for implementing a max value you can also use $$anonymous$$athf.$$anonymous$$in as such
timer = $$anonymous$$athf.$$anonymous$$in(timer+5,20);
This will pick the lowest number of the 2, so it will always be equal or less than 20
Your answer
Follow this Question
Related Questions
The timer starts after a collision with an object 1 Answer
How to work a real life timer? 2 Answers
addcount can't add when collide 0 Answers
the textures look bad 1 Answer