- Home /
How to have a Boolean check as true after multiple other Booleans are marked true
So I'm making a puzzle game and I was thinking of using a system where the puzzle pieces would check a boolean on a script when their coordinates matched up with the slot they're supposed to be in. Something like:
var NextLevel : Boolean;
var PosCorrect : Boolean
var P1 : GameObject;
var S1 : GameObject;
function Update(){
if(P1.position = S1.position)(
PosCorrect = true
}
if(multiple boolean = true){
NextLevel = True;
GUI();
}
And then I would have a GUI button show up that when clicked would let the player go onto the next level. Is there anyway to do this? And if there is how so.
You can check multiple conditions using the "Logical And" operator, &&
:
if (condition1 && condition2) {
doSomething();
}
If you have an array, you could check them using a loop:
var success = true;
for (var myBool in myBoolArray) {
success = success && myBool;
}
if (success) {
doSomething();
}
If you need more than that, it might help to clarify your data structure. How are you getting these booleans? Where are they co$$anonymous$$g from?
If branching or looping/arrays are new to you, now is probably a good time to look up some tutorials.
Answer by Kiwasi · Aug 09, 2014 at 08:35 PM
Use properties. In C# (I'm sure JavaScript has equivalent methods).
private bool[] boolArray;
private bool multiBool {
get {
for (int i = 0; i < boolArray.length; i++){
if (!boolArray[i]){
return false;
}
}
return true;
}
}
Checking multiBool will automatically loop through each bool in boolArray and only return true if all of the bools in the array are true.
Ok so when the multiBool is checked it checks all the other bools? And if they are all true then it ticks it as true.
I'll look into that thanks :D
It looks like there is a $$anonymous$$or typo that may cause confusion: Properties have both set
and get
accessors. If wanting to read the value then line three should be:
get {
Fixed. Nice catch.
$$anonymous$$y intention was to leave out the set as it has no meaningful implementation in this context.
Answer by Graham-Dunnett · Aug 09, 2014 at 08:32 PM
I'd think I'd have two static arrays Matched
and UnMatched
. Add all the puzzle pieces to the UnMatched
array when you start up. (Perhaps in Start()
for each game object.) Then as the player moves a piece to the right location, move the game object from the UnMatched
array to the Matched
array. You can then have code that looks at the number of entries in the UnMatched
array. Once it falls to zero, you know you have all the pieces in their correct locations.
See:
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics
if you need help understanding why the arrays should be static.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160228im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
boolean questions get, set, in java. 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Else and If 2 Answers
Pause Button Help 2 Answers
animation not working 0 Answers