- Home /
The question is answered, right answer was accepted
make increment and decreasement button with if statement didn't work?
!Hi, i have some mini project which it makes some increment and decreasement in it. so i make a button to make a soldier increase and decrease by pressing plus and min button. however , it didn't work , i try to debug but nothing happens. so maybe a void cannot using if statement ? please make it clear for me . thanks !
this the script i use: using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class CastleGo : MonoBehaviour {
public Text curInfantQty;
public Text curArchQty;
public Text curCavQty;
public int curInfant;
public int curArch;
public int curCav;
// Use this for initialization
void Start () {
curInfant = CastleQty.infantryQty;
curInfantQty.text = "" + curInfant;
curArchQty.text = "" + CastleQty.archerQty;
curCavQty.text = "" + CastleQty.cavalryQty;
}
public void plusInfant()
{
if (curInfant > CastleQty.infantryQty) {
curInfant++;
Debug.Log (curInfant);
} else {
return;
}
}
public void minInfant()
{
if (curInfant <= 0) {
curInfant--;
Debug.Log (curInfant);
} else {
return;
}
}
}
and the button i wish to make simply like this:
i already doing my stuff with the button setting like attach it to the script. still didn't figure it out.
Answer by Bonfire-Boy · Sep 01, 2016 at 02:21 AM
Looks to me like the logic is the wrong way round. You only decrease the value if it's already negative, and only increase it if it's already larger than the value you initialised it to. So, assuming that CastleQty.infantryQty is non-negative on initialisation, both plusInfant() and minInfant() are going to return without doing anything.
Also note that if you want the displayed value to change, you need to set curInfantQty.text again.
oh yeah , figure that one. but still didn't work. i think the void can't work with if statement? if i put the if statement , the debug.log didnt show up on debug console. but if i commented it out, the debug log works just fine. so im thinking the void can't work with if statement ? please clear this one out xD thanks
I'm sorry but it's not clear to me what you mean by "the void can't work with if statement". There is no "void" connected with your if statements, in fact the only "voids" in the code are return types of functions and they certainly won't impact on the logic within the functions.
But if you've changed the code and it still doesn't work, you should share the new code. We're not $$anonymous$$d-readers :)
never$$anonymous$$d i forgot to update it with text . sorry for bothering xD and how i can make your answer is the answer? cant do it through comment. thanks!
I've converted it to an answer you should be able to accept it now.
Follow this Question
Related Questions
I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers
I need to move a sprite when an UI button is clicked 1 Answer
How to keep your button selected after clicking away ??? 1 Answer
UI Button color 1 Answer
Hidden Canvas, doesn't disable interaction with buttons. 1 Answer