- Home /
adding a timer to game such that the time can be increased through points
i'm making a free runner game where the runner carries an object. i want to add a self destruct timer to this object such that the time would increase if the runner collects a coin.
Answer by 767_2 · Sep 25, 2014 at 11:10 AM
void Update()
{
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
destroy();
}
}
void OnCollisionEnter(Collision hit) {
if(hit.gameObject.tag=="coin")
timeLeft+=extraTime;
}
Really!! and how the hell can he understand to add variables? i think he is a rookie and don't know anything about scripting
Answer by malekbakeer · Sep 25, 2014 at 11:21 AM
make a coin objects and check the isTrigger [_] box in the inspector then add a new tag to them call it "Coin"
then add the following script to your runner
var timer : float;
var GameOver : boolean;
function Start ()
{
GameOver = false;
timer = 10;
}
function Update ()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
//do here the self destruct thing (i made a Restart after 1 second you can change)
Restart();
GameOver = true;
}
}
function Restart ()
{
yield WaitForSeconds (1);
Application.LoadLevel(0);
}
function OnGUI ()
{
if (GameOver == ture)
{
GUI.Label(Rect (Screen.width / 2, Screen.hieght / 2, 250, 100), "Game Over");
}
}
function OnTriggerEnter (coin : Collider)
{
if (coin.tag == "Coin")
{
//Change the number of how much time you wanna add when you collect the coin
timer += 2;
coin.Destroy;
}
}
Your answer
Follow this Question
Related Questions
Little Error on Timing 1 Answer
Timer limits 0 Answers
I want to have cancelinvoke start multiple seconds arfter object becomes invisible 1 Answer
Make Fillamount Match Timer 1 Answer
Timer Countdown fix. 2 Answers