Hey Everyone!
I am trying to make a kind of random event system in my city builder game, Using button.onclick.addlistener but clicking the buttons. I have a lot of events (2 shown here) so i need to use the same buttons for each different event. But Whenever i click the option buttons the results of option A is both the different events result. Giving money AND taking food for example. It seems like the addlistener is completely ignoring my if statements. What is going on here?
void RandomEvent()
{
int rEvent = Random.Range(1, 3);
if(rEvent == 1)
{
PopUp.active = true;
BeforeEvent.active = true;
AfterEvent.active = false;
BeforeEventText.text = "Your village is being attacked by Vikings! What do you do?";
Option1Text.text = "Send them to Valhalla!";
Option2Text.text = "Give them what they want";
EventOption1.onClick.AddListener(() => {
if(rEvent == 1)
{
AfterEvent.active = true;
BeforeEvent.active = false;
Citizens -= 3;
FoodI -= 50;
AfterEventText.text = "Well, That was an epic fail. You only had a couple farmers you never could have beaten them! They took a lot of food and kidnapped a couple people!";
}
else
{
Debug.Log("Event 1, A activated");
}
});
EventOption2.onClick.AddListener(() => {
if(rEvent == 1)
{
AfterEvent.active = true;
BeforeEvent.active = false;
FoodI -= 20;
AfterEventText.text = "They took some food, and left us alive for now..";
}
else
{
Debug.Log("Event 1, B activated");
}
});
}
if (rEvent == 2)
{
PopUp.active = true;
BeforeEvent.active = true;
AfterEvent.active = false;
BeforeEventText.text = "One of the nobles killed a fellow villager! He wants to pay you and the murderded victims family off if they keep queit, Or you can tell the other villagers and arrest him!";
Option1Text.text = "Accept payment";
Option2Text.text = "Report him";
EventOption1.onClick.AddListener(() => {
if(rEvent == 2)
{
AfterEvent.active = true;
BeforeEvent.active = false;
int rCoins = Random.Range(1, 300);
CoinI += rCoins;
happiness -= 20;
AfterEventText.text = "You accepted his payment and remained silent, was the money worth having a murderer walking free?";
}
else
{
Debug.Log("Event 2, A activated");
}
});
EventOption2.onClick.AddListener(() => {
if(rEvent == 2)
{
AfterEvent.active = true;
BeforeEvent.active = false;
happiness += 25;
AfterEventText.text = "You told the other villagers, they went mad in a rage and burned him alive. Well, at least they are happy";
}
else
{
Debug.Log("Event 2, B activated");
}
});
}
}