- Home /
How can I make this work when the booleans are equal?
When I run this code
public void checkForMatches() {
if(a && b
|| a && c
|| b && c
|| a && b && c){
matchFound();
}
}
void matchFound(){
Score += 10;
}
it happens the frame after the booleans aren't true anymore but when used in update it's very inaccurate and sometimes adds 20 or 30 to the score, fixedUpdate seems to do the same thing.
Isn't this just a case of setting the bools false as soon as you add score? Or call CheckFor$$anonymous$$atches only when the player interacts.
It is done like so:
an object has 3 objects connected to it when something happens with these they send "Cylinder$$anonymous$$essage(id)" to the parent object which will then according to the id integer set the a, b or c boolean to true.
done like so:
void FixedUpdate () {
Color objectColor = renderer.material.GetColor("_Color");
RaycastHit hit;
Vector3 angle0 = transform.TransformDirection(Vector3.up);
if (Physics.Raycast(transform.position, angle0, out hit, 2)){
Color otherObjectColor = hit.collider.renderer.material.GetColor("_Color");
//print("hit something");
if(objectColor == otherObjectColor){
if(objectColor == Color.green && otherObjectColor == Color.green){
Execute$$anonymous$$atches ("Green");
} else if(objectColor == Color.blue && otherObjectColor == Color.blue){
Execute$$anonymous$$atches ("Blue");
} else if(objectColor == Color.red && otherObjectColor == Color.red){
Execute$$anonymous$$atches ("Red");
}
}
}
}
void Execute$$anonymous$$atches(string cylColor) {
$$anonymous$$olecule molecule;
molecule = transform.parent.gameObject.GetComponent<$$anonymous$$olecule>();
if(cylColor == "Green"){ molecule.Cylinder$$anonymous$$essage(1); }
if(cylColor == "Blue"){ molecule.Cylinder$$anonymous$$essage(2);}
if(cylColor == "Red"){ molecule.Cylinder$$anonymous$$essage(3);}
}
then in "molecule.cs"
public void Cylinder$$anonymous$$essage(int id){
switch(id){
case 1:
a = true;
break;
case 2:
b = true;
break;
case 3:
c = true;
break;
}
}
Is there any specific reason why you can't set a, b and c to false after adding points?
void matchFound(){
Score += 10;
a = false;
b = false;
c = false;
}
Also, the last part of the 'if' is not necessary:
a && b && c
Since you have already checked a&&b, b&&c and a&&c, either a true result has already been found, or a&&b&&c is guaranteed to be false
The problem is that I need it to happen on the frame it happens, not afterwards or when it's set back to false but I need it to happen only once and the update in unity continiously puts 20 or 30 ins$$anonymous$$d of 10.
EDIT: I seem to have fixed it, I don't know how but it seems to be fixed >_<
Answer by Suddenly_Bacon · Sep 01, 2014 at 04:42 PM
Maybe
if(a && b || a && c || b && c || a && b && c)
should be
if((a && b) || (a && c) || (b && c) || (a && b && c))
This will make it so that the AND statements will be calculated first, then the OR statements will correctly compare the AND statements.