- Home /
The question is answered, right answer was accepted
Lowering a variable 1 number lowering it entirely?
I have this script that triggers a bomb explosion when clicked on with the left mouse button. When you click the right mouse button, it blows up all the bombs, but you only have a limited number of right mouse button clicks before that function is disabled. Unfortunately, the number goes down by however many bombs were blown up, instead of just once per use of the right mouse button. How could I fix that? My code seems sound to me. The GUI that is used to keep track of the "nukes" I have left is in the HUD script.
var prefab : Rigidbody;
var Stuff : Transform;
function OnMouseDown() {
HUD.Score += 1;
Destroy(gameObject);
var Spawn : Rigidbody;
Spawn = Instantiate(prefab, Stuff.position, Stuff.rotation);
}
function Update() {
if(renderer.isVisible)
{
if(HUD.Nukes >= 0)
{
if(Input.GetMouseButtonDown(1))
{
HUD.Nukes -= 1;
Destroy(gameObject);
var Spawn : Rigidbody;
Spawn = Instantiate(prefab, Stuff.position, Stuff.rotation);
}
}
}
}
Any help would be appreciated! Thanks!
Answer by ozone · May 23, 2014 at 12:00 AM
I take it the script you provided is attached to the bombs themselves, which is the problem. It means that each bomb is separately lowering the number instead of lowering it by one in total. I would recommend removing the line HUD.Nukes -= 1;
and adding a statement to the HUD script itself that lowers the variable by one each time the right mouse button is pressed.
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Changing 3d text through script 1 Answer
Gui Text Script 4 Answers
GUI Button Disappearing 1 Answer
GUI.Box NullReferenceException 0 Answers