- Home /
Simple Boolean Logic/Formatting Question
This has to be something simple, but I am new to Java and still learning, I've made it this far without having to ask a personal question but it's got me stumped.
I have three boxes, when they collide with my bullet the boolean should switch to false, when all three boxes have switched their assigned boolean to false the player should be taken to the game over screen. With Debug.Log I am able to see it's reading them as false, but it isn't switching them over in the inspector. So I feel like I might be instantly setting it back somehow. Here is all of my code for this particular script.
Edit:I should have clarified, updating the original post. It is attached to the prefab that is my bullet. So, all of these happen when my bullet experiences a collision. There is only one script.
static var Counter : int = 0;
var object : GameObject;
var success : AudioClip;
var thud : AudioClip;
var explosion : GameObject;
var fail : AudioClip;
var boxhit1 : boolean = true;
var boxhit2 : boolean = true;
var boxhit3 : boolean = true;
function OnCollisionEnter (theCollision : Collision){
if(theCollision.rigidbody){
Instantiate(explosion, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint(fail, Camera.main.transform.position);
Destroy(object);
}
else if(theCollision.gameObject.name == ("Box1") && boxhit1){
theCollision.gameObject.AddComponent(Rigidbody);
gameObject.Find("Box1").renderer.material.color = Color.black;
Instantiate(explosion, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint(success, Camera.main.transform.position);
Counter++;
boxhit1=!boxhit1;
Debug.Log("Box Hit 1: " + boxhit1);
Destroy(object);
}
else if(theCollision.gameObject.name == ("Box2") && boxhit2){
theCollision.gameObject.AddComponent(Rigidbody);
gameObject.Find("Box2").renderer.material.color = Color.black;
Instantiate(explosion, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint(success, Camera.main.transform.position);
Counter++;
boxhit2 = false;
Debug.Log("Box Hit 2: " + boxhit2);
Destroy(object);
}
else if(theCollision.gameObject.name == ("Box3") && boxhit3){
theCollision.gameObject.AddComponent(Rigidbody);
gameObject.Find("Box3").renderer.material.color = Color.black;
Instantiate(explosion, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint(success, Camera.main.transform.position);
Counter++;
boxhit3 = false;
Debug.Log("Box Hit 3: " + boxhit3);
Destroy(object);
}
else if(theCollision.gameObject){
Instantiate(explosion, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint(thud, transform.position);
Destroy(object);
}
}
function Update(){
if(!boxhit1 && !boxhit2 && !boxhit3){
Debug.Log("All are false!");
Application.LoadLevel("GameOver");
}
}
That's not Java, it's Unity Script. And Unity Script is not Javascript, although it's syntactially nearly identical. You can thank Unity, and the people who changed LiveScript to Javascript for the confusion.
You should know this before you try debugging ;)
That is definitely ter$$anonymous$$ology I'll keep in $$anonymous$$d, I wasn't sure how to properly describe it,
What's this script attached to? In particular, is there more than one of it? I ask because each instance of your script is going to have its own copies of boxhit1
, boxhit2
, and boxhit3
.
I should have clarified, updating the original post. It is attached to the prefab that is my bullet. So, all of these happen when my bullet experiences a collision. There is only one script.
You don't ever have to duplicate code. You should be doing something like this ins$$anonymous$$d of duplicating the same code for every box:
function OnCollisionEnter(theCollision:Collision) {
boxHit1 = CheckBoxCollision(theCollision, "Box1", boxHit1);
boxHit2 = CheckBoxCollision(theCollision, "Box2", boxHit2);
boxHit3 = CheckBoxCollision(theCollision, "Box3", boxHit3);
}
function CheckBoxCollision(collision:Collision, name:String, boxHit:boolean):boolean {
if (boxHit && collision.gameObject.name == name) {
//all your code in relation to 'collision', 'name' and 'boxHit'
}
return boxHit;
}
This makes for much nicer and more readable code :)
Answer by Slipperz · Oct 22, 2013 at 12:19 AM
Well, I figured it out. Thanks to rutter I realized a single individual bullet would have to hit all three for this to work. So right in front of my face. Thank you! I suppose the next line of action would be to tell a separate script to shift booleans on an gameObject that doesn't destroy itself.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120402im_/https://answers.unity.com/themes/thub/images/avi.jpg)