- Home /
Upgrade once per click on Button c#
Hi all,
my problem is maybe just small. I want to go up an age once the player reached an amount of exp. I have one button to click, and connected the function to it. But if i click the button, it goes through all, und it fits.
How can i stop it, once it reached the first statement even if i could upgrade more than 1 times?
public void GoAgeUp()
{
if (GM.exp >= 500) {
Debug.Log ("egypts age");
GM.age = 2;
EDP.StoneAgeUnitsPanel.SetActive (false);
EDP.StoneAgeTurretsPanel.SetActive (false);
}
if (GM.exp >= 8000) {
Debug.Log ("medieval age");
GM.age = 3;
EDP.EqyptsUnitsPanel.SetActive (false);
EDP.EqyptsTurretsPanel.SetActive (false);
}
if (GM.exp >= 12000) {
GM.age = 4;
}
if (GM.exp >= 16000) {
GM.age = 5;
}
if (GM.exp >= 20000) {
GM.age = 6;
}
}
Thanks in advance.
public void GoAgeUp()
{
if (G$$anonymous$$.exp >= 500) {
Debug.Log ("egypts age");
G$$anonymous$$.age = 2;
EDP.StoneAgeUnitsPanel.SetActive (false);
EDP.StoneAgeTurretsPanel.SetActive (false);
}
else if (G$$anonymous$$.exp >= 8000) {
Debug.Log ("medieval age");
G$$anonymous$$.age = 3;
EDP.EqyptsUnitsPanel.SetActive (false);
EDP.EqyptsTurretsPanel.SetActive (false);
}
else if (G$$anonymous$$.exp >= 12000) {
G$$anonymous$$.age = 4;
}
else if (G$$anonymous$$.exp >= 16000) {
G$$anonymous$$.age = 5;
}
else if (G$$anonymous$$.exp >= 20000) {
G$$anonymous$$.age = 6;
}
}
That's what i tested before, but it just stops in the first statement and stays there. $$anonymous$$y Button is a new Unity 5 button. Any known issues?
Answer by barbe63 · Jun 19, 2015 at 03:36 PM
public void GoAgeUp()
{
if (GM.exp >= 20000) {
GM.age = 6;
}
else if (GM.exp >= 16000) {
GM.age = 5;
}
else if (GM.exp >= 12000) {
GM.age = 4;
}
else if (GM.exp >= 8000) {
Debug.Log ("medieval age");
GM.age = 3;
EDP.EqyptsUnitsPanel.SetActive (false);
EDP.EqyptsTurretsPanel.SetActive (false);
}
else if (GM.exp >= 500) {
Debug.Log ("egypts age");
GM.age = 2;
EDP.StoneAgeUnitsPanel.SetActive (false);
EDP.StoneAgeTurretsPanel.SetActive (false);
}
}
Really? Why? I mean, i will test this!
Ah i think i know why, since the first condition is met, it wont go further. Interesting.
Sadly, doesn't work as i want, i don't want to skip any statement. I want to goe through all. Any idea?
Let me understand it good. You need a button that will upgrade your age so why don't you just make G$$anonymous$$.age++. Then you can just make a switch on G$$anonymous$$.age to do your functions. You can then make the button to be interactable only if the experience is enough with a simple script. If this is what you want and you need help to achieve this I can help you on that.
What you want is not clear enough, either you need exp and then the method I provided you is good either you need a button to upgrade and do as I just said.
I want to upgrade only when the player has >500exp >8000 and so on. But don't want to skip the 500 one when the player already have 9000 exp but not upgraded yet. How would i build this up? Since i can't use case ranges.
Ok now I think i got it.
If i understood it well what you want is more something like this:
public void GoAgeUp()
{
if (G$$anonymous$$.exp >= 500 && G$$anonymous$$.age==1) {
Debug.Log ("egypts age");
G$$anonymous$$.age = 2;
EDP.StoneAgeUnitsPanel.SetActive (false);
EDP.StoneAgeTurretsPanel.SetActive (false);
}
else if (G$$anonymous$$.exp >= 8000 && G$$anonymous$$.age==2) {
Debug.Log ("medieval age");
G$$anonymous$$.age = 3;
EDP.EqyptsUnitsPanel.SetActive (false);
EDP.EqyptsTurretsPanel.SetActive (false);
}
else if (G$$anonymous$$.exp >= 12000 && G$$anonymous$$.age==3) {
G$$anonymous$$.age = 4;
}
else if (G$$anonymous$$.exp >= 16000 && G$$anonymous$$.age==4) {
G$$anonymous$$.age = 5;
}
else if (G$$anonymous$$.exp >= 20000 && G$$anonymous$$.age==5) {
G$$anonymous$$.age = 6;
}
}
Answer by Flightkick · Jun 19, 2015 at 03:41 PM
When you separate all if statements then every if statement will be checked. To ensure that only one code path will be executed use 'else if'. Furthermore you can invert order in which they are checked. For example if I have 9000 exp I should be in the 'medieval age' but since your first logical condition checks if my exp >= 500 it will trigger that code path. By inverting it you will always get the highest possible rank based on your exp.
public void GoAgeUp()
{
if (GM.exp >= 20000)
{
GM.age = 6;
}
else if (GM.exp >= 16000)
{
GM.age = 5;
}
else if (GM.exp >= 12000)
{
GM.age = 4;
}
else if (GM.exp >= 8000)
{
Debug.Log("medieval age");
GM.age = 3;
EDP.EqyptsUnitsPanel.SetActive(false);
EDP.EqyptsTurretsPanel.SetActive(false);
}
else if (GM.exp >= 500)
{
Debug.Log("egypts age");
GM.age = 2;
EDP.StoneAgeUnitsPanel.SetActive(false);
EDP.StoneAgeTurretsPanel.SetActive(false);
}
}
Thanks alot, i think it will fix the issue, i will test that soon, thanks for your explaination. Cheers.
I want to achieve that no age is skipable, if i have 9000 exp it goes instandly to Gm.age = 3. How can i achieve this?
Answer by jdraper3 · Jun 19, 2015 at 08:53 PM
Create a class to describe your game age:
public class GameAge
{
public int ExpRequired { get; set; }
public GameObject UnitsPanel { get; set; }
public GameObject TurrentsPanel { get; set; }
public string Name { get; set; }
}
Create a variable in your game manager that keeps track of the current age, and also a list of all of your ages in the game:
GameManager.Ages = new List<GameAge>();
GameManager.Ages.Add(new GameAge()
{
ExpRequired = 0,
UnitPanel = EDP.StoneAgeUnitsPanel,
TurretsPanel = EDP.StoneAgeTurretsPanel,
Name = "Stone Age"
});
GameManager.Ages.Add(new GameAge()
{
ExpRequired = 500,
UnitPanel = EDP.EgyptsUnitsPanel,
TurretsPanel = EDP.EgyptsTurretsPanel,
Name = "Egypt Age"
});
// Add all of your ages...
GameManager.CurrentAge = 0;
Add a method to your GameManager that sets the current age by looping through the ages and skiping the ones that have already been activated:
public void ProgessToCurrentAge()
{
for (var x = CurrentAge; x < Ages.Count(); x++)
{
if (Ages[x].ExpRequired <= CurrentExperience)
{
Ages[CurrentAge].UnitsPanel.SetActive(false);
Ages[CurrentAge].TurretsPanel.SetActive(false);
CurrentAge = x;
Debug.Log(Ages[CurrentAge].Name + " active.");
}
}
}
I don't know if this will work as-is - I can't test it right now unfortunately, but it should be close at least.
Thanks for your affort but i already have the solution, in the first answer.
Not a problem, I didn't see the answer earlier because it was hidden. Glad you got it sorted out! :)
Your answer
Follow this Question
Related Questions
GUI Repeatbutton up? 1 Answer
Unity UI Button not working 1 Answer
Stop clicking through a GUI window 2 Answers
Default Android touch sound 0 Answers
Help? Problem with GUI Text button. 1 Answer