- Home /
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.
Your question is very confusing, can you show the entire script and maybe a view on how it is setup in your inspector so we can get an idea of the structure?
Ideally you would make an array of CostInformation[] which contain the decisions (could be assigned in inspector).
Are the costgold1,costgold2 and other part of this script?? How many instances do you want of each of the set. A set being costgold,costsoldiers............costlumber
Answer by Roger_0123 · Jun 07, 2020 at 08:52 AM
I ended up rewriting everyhing :) 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 :) 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
}
}
I don't know how to read this code. I can implement it to the point where it says "statmanager does not contain a definition for length."
also what does (stat$$anonymous$$anager.lenght > 0 && OptionAvailabledecisions[0].GetComponent())) mean? I kinda get the stat$$anonymous$$anager.length > 0 asking if we're using the first stat$$anonymous$$anager, but there's only one stat manager, that tells us what out stats are and gives that information to the player. But what happens with the OptionAvailable? Is it automatically checking for an 'is true' status?
If you have a decent C# tutorial to recommend that would be great, but I keep finding ones that are out of date, or that just end half way through.
Oh I'm sorry, that's my error! I mean decisions.length
I thought you might have, but when I try that nothing happens. No errors but also no... anything.
Yes, OptionAvailable takes the choice cost (coin and so on) and returns true if the choice can be applied (so if you have enough coins, farms... it's just the condition you inserted before). For tutorial on unity, I highly recommend Brackeys youtube channel for very efficient and simple tutorials. I found something basic about C# arrays here: W3school
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Spell-casting combo system 1 Answer
Using an int to find the index of a GameObject array 1 Answer
How do I change gameObject values in an array all at once? 1 Answer