- Home /
Countdown by pressing a button.
How can I add a countdown in this script, by pressing the left mouse button? Every click counts minus 1. 10,9,8,7,6,5,4,3,2,1,0 example. ? This currently does not work in this script does not understand the problem.
#pragma strict
var myTrigger : GameObject;
var myObject : GameObject;
var countAmmo : int = 10 ;
private var score : int = 10;
var guiScore : GUIText;
function Start ()
{
guiScore.text = "Score: 10";
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
countAmmo = countAmmo -1;
score = countAmmo -1;
if(countAmmo == 0)
if(score == -1)
{
myObject.SetActive(false);
}
else
{
guiScore.text = "Score: -1";
myObject.SetActive(true);
}
// score -= 10;
// guiScore.text = "Score: " + score;
}
Answer by HarshadK · May 04, 2015 at 09:41 AM
Your else loop should be:
else
{
score--;
guiScore.text = "Score: " + score.ToString();
myObject.SetActive(true);
}
Also everything below if(Input.GetButtonDown("Fire1")) should be placed inside { } in order for all that to work when left mouse button is pressed.
Now on score: 10 numbers count as a stopwatch. I also have 3 errors
1: else.Unexpected token 2 UCE0001: ';' expected. Insert a semicolon at the end. 3 BCE0044: expecting EOF, found '}'.
#pragma strict
var myTrigger : GameObject;
var myObject : GameObject;
var countAmmo : int = 10 ;
private var score : int = 10;
var guiScore : GUIText;
function Start ()
{
guiScore.text = "Score: 10";
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
else
{
score--;
guiScore.text = "Score: " + score.ToString();
myObject.SetActive(true);
}
}
}
You're missing an if your your else inside mouse button down.
Check this script:
#pragma strict
var myTrigger : GameObject;
var myObject : GameObject;
var countAmmo : int = 10;
private var score : int = 10;
var guiScore : GUIText;
function Start ()
{
guiScore.text = "Score: " + score.ToString();
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
if(score == -1)
{
myObject.SetActive(false);
}
else
{
score--;
guiScore.text = "Score: " + score.ToString();
myObject.SetActive(true);
}
}
}
Answer by ForeignGod · May 04, 2015 at 09:45 AM
Never mind, look at HarshadK for a simpler answer ^^
Your answer
Follow this Question
Related Questions
Script for counting not working properly 1 Answer
How to keep score? 1 Answer
InputField Button didn't work on method 0 Answers
the count remains the same 0 Answers
Central score counter for 3 score generating buttons 1 Answer