Checking bool on multiple script instances.

I’m making a choice based game where each “Decision Panel” has a bool that is ticked when a valid decision is made. What I want is a way for my “EventManager” to detect when all those bools are ticked.

I’d use a standard getcomponent for each “Decision Panel” but not every scene will have the same number of decisions to be made, so I feel like an array would be more tidy, but arrays really do my head in. (I can never seem to get them to work correctly and when I try a for loop is always comes up with OutOfRange)

// Start is called before the first frame update
void Start()
{
    canvas = GameObject.FindGameObjectWithTag("MainCanvas");
}

// Update is called once per frame
void Update()
{
    gold = canvas.GetComponent<StatManager>().Gold;
    soldiers = canvas.GetComponent<StatManager>().Soldiers;
    citizens = canvas.GetComponent<StatManager>().Citizens;
    food = canvas.GetComponent<StatManager>().Food;
    farms = canvas.GetComponent<StatManager>().Farms;
    lumber = canvas.GetComponent<StatManager>().Lumber;

    if(decision1 == null)
    {
        costGold1 = 0;
        costSoldiers1 = 0;
        costFood1 = 0;
        costFarms1 = 0;
        costCitizens1 = 0;
        costLumber1 = 0;
    }
    else
    {
        costGold1 = decision1.GetComponent<CostInformation>().costGold;
        costSoldiers1 = decision1.GetComponent<CostInformation>().costSoldiers;
        costFood1 = decision1.GetComponent<CostInformation>().costFood;
        costFarms1 = decision1.GetComponent<CostInformation>().costFarms;
        costCitizens1 = decision1.GetComponent<CostInformation>().costCitizens;
        costLumber1 = decision1.GetComponent<CostInformation>().costLumber;
    }
    if (decision2 == null)
    {
        costGold2 = 0;
        costSoldiers2 = 0;
        costFood2 = 0;
        costFarms2 = 0;
        costCitizens2 = 0;
        costLumber2 = 0;
    }
    else
    {
        costGold2 = decision2.GetComponent<CostInformation>().costGold;
        costSoldiers2 = decision2.GetComponent<CostInformation>().costSoldiers;
        costFood2 = decision2.GetComponent<CostInformation>().costFood;
        costFarms2 = decision2.GetComponent<CostInformation>().costFarms;
        costCitizens2 = decision2.GetComponent<CostInformation>().costCitizens;
        costLumber2 = decision2.GetComponent<CostInformation>().costLumber;
    }

public void Option1()
{
    if (costGold1 <= gold && costSoldiers1 <= soldiers && costCitizens1 <= citizens && costFood1 <= food && costFarms1 <= farms && costLumber1 <= lumber)
    {
        decisionINT = 0;
        DecisionMade = true;
        Checkmark[0].text = "X";
        Checkmark[1].text = "";
        Checkmark[2].text = "";
        Checkmark[3].text = "";
    }
}

as you can see, it’s very very repetitive (I’ve cut a lot out, but I’ve iterated it 4 times and limited each decision to a max of 4 options, because I couldn’t get an option array working.) but my Event Manager script is blank, and I don’t want it to end up looking like this.

My hope is that if I can learn a little bit about correctly manipulating arrays with this Collecting all the Bools of an array of objects I can use that knowledge to make this script look neater.

I ended up rewriting everyhing :slight_smile:
I thought checking for resources in an Update it’s useless and expensive. Also calling GetComponent so many times is expensive. I suggest you to look for some C# tutorials, as a polished code can save you a lot of headaches :slight_smile: Just come in mind: Also check Checkmark.length >= 4 before setting its elements. Hope this helps!

GameObject canvas;
StatManager statManager;
public GameObject[] decisions; //an array of GAmeObject which can be set from inspector

// Start is called before the first frame update
 void Start()
 {
    canvas = GameObject.FindGameObjectWithTag("MainCanvas");
    statManager = canvas.GetComponent<StatManager>(); //Calling GetComponent<StatManager>() so many times in Update() is really useless and expensive :)
 }

/*Called to choose if this option can be picked*/
 bool OptionAvailable(CostInformation costInformation){
     if(costInformation){//Check if not null
        //if not null compare if resources are greater than its cost
        return costInformation.costGold <= statManager.Gold && costInformation.costSoldiers <= statManager.Soldiers && costInformation.costCitizens <= statManager.Citizens && costInformation.costFood <= statManager.Food && costInformation.costFarms <= statManager.Farms && costInformation.costLumber <= statManager.Lumber;
     }
     else{
        return false;
     }
 }

 public void Option1()
 {
     /*Checking if not going IndexOutOfBounds and passing the right CostInformation*/
     if (decisions.length > 0 && OptionAvailable(decisions[0].GetComponent<CostInformation>()))
     {
         /*then do something*/
         decisionINT = 0;
         DecisionMade = true;
         Checkmark[0].text = "X";
         Checkmark[1].text = "";
         Checkmark[2].text = "";
         Checkmark[3].text = "";
     }
 }

 public void Option2()
 {
     if (decisions.length > 1 && OptionAvailable(decisions[1].GetComponent<CostInformation>()))
     {
         //something
     }
 }