- Home /
Trying to stop or change an if
Hello, I think this is simple but my headeach does not let me see the answer hahaha. Ok I made this to consume the energy bar while the player press Shift. But if the player press Shift the energy bar gets 0, starts to grow to the oposite direction, all i want is to stop this "if effect" to the GUI when its width is = 0.
#pragma strict
var GUIEnergy : GUITexture;
function Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
GUIEnergy.pixelInset.width -= 0.3;
}
}
Answer by AlucardJay · Apr 23, 2013 at 10:12 PM
Either : check if the pixelInset is greater than zero plus 0.3 before removing 0.3
Or : after removing 0.3, check if pixelInset is below zero. If so, make pixelInset zero
Either :
#pragma strict
var GUIEnergy : GUITexture;
function Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
if ( GUIEnergy.pixelInset.width >= 0.3 )
{
GUIEnergy.pixelInset.width -= 0.3;
}
}
}
Or :
#pragma strict
var GUIEnergy : GUITexture;
function Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
GUIEnergy.pixelInset.width -= 0.3;
if ( GUIEnergy.pixelInset.width < 0 )
{
GUIEnergy.pixelInset.width = 0;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gamepad input enum eType not working 0 Answers
Backspace in limited length textfield causes ArgumentOutOfRangeException 1 Answer
Help! Hiding/showing GUITextures! 1 Answer
Android Textfield locks out input until esc 2 Answers
Shot Counter Question 2 Answers