Enemy's health script add points (*2) when collide with 2 BulletPlayer at the same time

Hi all,

I have a little issue with my enemy’s health script.
My player shots a ‘bullet prefab’ from two shotspawns in front of him.
When the enemy is hit and die by colliding two bulletplayer AT THE SAME TIME, the score value is given twice…
Is there a proper way to avoid that ?

if (life <= 0) 
{
ScoreManager.score += ScoreValue; 
Destroy(this.gameObject); 
GameObject.Instantiate(explosion, this.gameObject.transform.position, this.gameObject.transform.rotation);
}

Thanks for your help !

I’d probably use something like this:

int oldLife = life;
life -= ... // reduce life value
if(oldLife > 0 && life <=0)
{
    //...
}

Since the code should technically still be executed for one bullet after the other (unless you’re using some kind of unsafe threading) this should only allow it to be executed once.

I test this code below and it do the job !
I can see 2 or 3 debug.log in the console when i use my ‘autofire’ shot :slight_smile:
I declare a count variable and use it in a IF statment that increase the ‘count’ value :

private int count;

 if (life <= 0)
    {
    if (count == 0)
    {
    ScoreManager.score += ScoreValue;
    Destroy(this.gameObject);
    GameObject.Instantiate(explosion, this.gameObject.transform.position, this.gameObject.transform.rotation);
    count++;
    }
    else
    {
    Debug.Log ("Touched but destruction is already in progress");
    }
    }