Score counted multiple times when several projectiles impact the same target

Hello!

I am working on a top down arcade game, and i ran into an issue that consists of the following unwanted behaviour:

  • In a nominal case and as an example, when one bullet that does 1dmg hits a target with 1 hp and a score value of 10, everything goes well, (the bullet hits, applies the damage, targets life = 0 so the target is destroyed and 10 points added to the score)
  • in the glitch case, if 5 of these same bullets hits the same target at the same time, then instead of 10 points, 40 points are added to the score (also 4 explosions vfx are instantiated, and i would say the target is destroyed “4 times more too”) .
    Any ideas ? Thanks !

The OnImpactBehaviour script is attached to the ennemies, and the projectiles only call ReceiveDamage function when the 2 colliders collides. Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OnImpactBehaviour : MonoBehaviour
{
    public int hitpoints;
    public int scoreValue;
    public GameObject explosion;

    private GameController gameController;


    private void Start()
    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameController == null)
        {
            Debug.Log("Cannot find 'GameController' script");
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            Instantiate(explosion, collision.transform.position, collision.transform.rotation);
            Instantiate(explosion, transform.position, transform.rotation);
            //gameController.GameOver();
            Destroy(collision.gameObject);
            gameController.SubstractOneLife();
            Destroy(gameObject);
        }
    }

    public void ReceiveDamage(int damageReceived)
    {
        hitpoints -= damageReceived;
        if (hitpoints <= 0)
        {
            Instantiate(explosion, transform.position, transform.rotation);
            gameController.AddScore(scoreValue);
            Destroy(gameObject);
        }
    }
}

Gregg - I’m not able to add code right now, but you might try adding a flag that you set the first time a projectile collides with the object, and then only updates score/explosions, etc., only if that flag is not set already.

You could also disable collisions on the object after the first collision, so further projectiles don’t register when then come into contact with the object.