Question by
wilfy · Feb 05, 2018 at 09:00 PM ·
colliderontriggerenter
[HELP] OnTriggerEnter2D with 2 different missile types colliding with player PROBLEM
Hi, i have seen similar issues here but they seem to be slightly different answers to what I need, so please help me!! Below is my code so far, it should be self explanatory (sorry if its confusing to actual coders, I'm quite new to this :) )
I know the nested If statements are a terrible idea, but the issue i am trying to resolve is setting my "collider" to be EITHER missile OR torpedo. What am i doing wrong?
void OnTriggerEnter2D(Collider2D collider){
if (collider = GameObject.FindGameObjectWithTag ("Missile")) {
enemyProjectile missile = collider.gameObject.GetComponent<enemyProjectile> ();
if (missile) {
health -= missile.Damage ();
missile.Hit ();
if (health <= 0) {
Die ();
AudioSource.PlayClipAtPoint (playerExplosionSFX, transform.position);
}
}
} else if (collider = GameObject.FindGameObjectWithTag ("Torpedo")) {
HomingTorpedo torpedo = collider.gameObject.GetComponent<HomingTorpedo> ();
if (torpedo) {
health -= torpedo.GetTorpedoDamage ();
torpedo.torpedoHit ();
if (health <= 0) {
Die ();
AudioSource.PlayClipAtPoint (playerExplosionSFX, transform.position);
}
}
}
}
Comment
For goodness sake, sorry, stupid question. remove the "collider =" and we're golden.