- Home /
Pass bool value from multiple objects to one file to check their values?
I have 4 cubes, when I click on a cube its position goes up and a boolean isRaised changes to true. When I click it again the cube goes back to its original spot and the boolean goes back to false. Is there a way I can click a gui button to see if all the cubes are up at one time?
I have created the cubes. I have created individual scripts for each cube and attached it to the cube so they all have unique booleans. So each cube has its own name and its own script attached to it and nothing else. What additional steps do I need to take to get this to work? Or, are there any other ways to solve this problem?
Thanks.
Answer by Justin Warner · Apr 14, 2011 at 02:21 AM
Give the cubes their own tag, and loop through them, check the countTotal to countUp.
var boxes : GameObject[]; var nextLevelCount = 0; var boxUpCount = 0; var boxTotalCount = 0; boxes = GameObject.FindGameObjectsWithTag("Box");
//.boxUp is the boolean in the script with the boolean of whether it's up or not.
for (var box : GameObject in boxes) { if(box.GetComponenet(SCRIPTNAMEHERE).boxUp == true) { boxUpCount++; } boxTotalCount++;
if(box.ToString == "Box1")
{
nextLevelCount++;
}
if(box.ToString == "Box5")
{
nextLevelCount++;
}
}
if(boxUpCount == boxTotalCount) { //Do whatever when all up. }
if(nextLevelCount == 2) { //Load scene. }
Not tested, but should work/get the point across =) Good luck!
Thank you very much for your quick reply. Is there a way I can check to see which specific boxes are up though? It is going to be part of a $$anonymous$$i game and I actually want to be able to set a certain combination. Like the first and the third box up and the second and fourth down would allow the user to go to a new scene. Does that make sense?
What if, I just change the boolean to a global var, can I then, access each individual script to see if it is true or not? would that work?
You can get the actual name of the game object with: http://unity3d.com/support/documentation/ScriptReference/Object.ToString.html?from=GameObject And with that, compare the name with one of the things in the for:each loop, and have a counter that counts when it's good, and then do a compare at the bottom... I'll edit, 1 sec.
If that makes sense... And you can just compare the names =).