- Home /
Unity projectile damaging twice before destroyed
In the game I have a projectile which can be fired by the player, and when it hits the enemy its set to do one damage. The problem is that it for some reason triggers the on collision enter trigger twice before its destroyed. How do I fix this?
Code for the projectile: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FireballController : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D collision)
{
// Destroy object if hitting anything
if (collision.CompareTag("Player"))
{
}
else
{
Destroy(gameObject);
}
}
}
code for the enemies:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int health;
void Update()
{
if (health <= 0)
{
Destroy(gameObject);
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Fireball"))
{
health--;
Debug.Log("Au");
Destroy(collision.attachedRigidbody.gameObject);
}
}
}
Answer by shadowpuppet · Sep 24, 2019 at 09:52 PM
That happens to me a lot whee events happen twice, pickups, damage etc - sometimes even instantiating ragdolls to replace dead character when he dies. I simply add a bool
if ( dead==false){
do what you want here
dead=true;
}
this would work to set the characters to dead, but my problem is that they will deal damage twice with one projectile. Using this system I would have to have a timer for it to allow the next projectile to deal its damage as intended.
Your answer
![](https://koobas.hobune.stream/wayback/20220612212546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to trigger a specific collider ? 1 Answer
Changing layers affect triggers? 1 Answer
Adds too many points to the score? 0 Answers
How do you execute Trigger-collider collision only in one gameobject? 1 Answer