- Home /
Question by
fernandaputra45 · Apr 07, 2019 at 09:09 AM ·
if statementsimple
Can I Simply This Code
I have 6 boolean and condition which is almost same each other. Can i simply my code?
void OnTriggerEnter(Collider coll){
if (coll.gameObject.tag == "Pbiru"){
Destroy (this.gameObject);
if (destroy1 == true) {
Destroy (hit1.collider.gameObject);
}
if (destroy2 == true) {
Destroy (hit2.collider.gameObject);
}
if (destroy3 == true) {
Destroy (hit3.collider.gameObject);
}
if (destroy4 == true) {
Destroy (hit4.collider.gameObject);
}
if (destroy5 == true) {
Destroy (hit5.collider.gameObject);
}
if (destroy6 == true) {
Destroy (hit6.collider.gameObject);
}
}
}
Comment
Best Answer
Answer by Tsaras · Apr 07, 2019 at 10:14 AM
You can only simplify this by using Arrays, by grouping the hit objects and the destroy booleans in an array and looping through them in 4 or 5 lines of code. Something like this:
GameObject[] hit; //array of gameobjects
bool[] destroy; //array of bools
void OnTriggerEnter(Collider coll){
if (coll.gameObject.tag == "Pbiru"){
Destroy (this.gameObject);
for(int i = 0; i < destroy.Length ; i++)
if (destroy[i])
Destroy (hit[i].collider.gameObject);
}
}
}
Your answer
