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 (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);
}
}
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);
}
}
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.