Skip OnTriggerEnter isn't working at all
I'm following this tutorial: https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial/extending-space-shooter-enemies-more-hazards?playlist=17147
In summarize my problem, when enemy fires bullet, it blew up itself because the bullet hits it's collider.
The funny thing is that enemy has "Enemy" tag, and I skipped every hit related logic inside of OnTriggerEnter of enemy bullet like this:
void OnTriggerEnter(Collider other) {
if(other.CompareTag("Boundary") || other.CompareTag("Enemy")) return;
if(explosion != null) {
Instantiate(explosion, transform.position, transform.rotation);
}
// if(other.tag == "Player") {
if(other.CompareTag("Player")) {
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
else {
gameController.AddScore(scoreValue);
}
Destroy(other.gameObject);
Destroy(gameObject);
}
But it still blowing up itself. I couldn't find any problem in my code, seems fine. I manually printed that what was hit, and it saids it was "Enemy".
What is happening here? Am I missing something? Why bullet doesn't follow my code and blew the enemy ship?
Any help will be very thanksful.
hello i would really love to help ! can i please have a sample of your project so i can try figure out whats wrong ?
Answer by BuzaMahmooza · Jun 11, 2018 at 02:41 AM
I'm assuming that this is the script attached to the bullet gameObject. In line 16, Destroy(other.gameObject);
will destroy whatever the bullet collides with because it's not in the IF-statement. Move line 16 to be inside the IF-statement and then it should only destroy the
if(other.CompareTag("Player")) {
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
gameController.GameOver();
}