I have gates in my game that cause an explosion on each of their two edges (identical to this: Geometry Wars 2: Pacifism: 20.1B - YouTube).
On an enemy death, I want to call the die() function of the EnemyController. So I added a circle collider to my GateExplosion, and use this simple code:
private void OnTriggerEnter2D(Collider2D collider2D)
{
if (collider2D.CompareTag("Enemy"))
{
EnemyController c = collider2D.gameObject.GetComponent<EnemyController>();
c.die();
}
}
Code from EnemyController:
public void die()
{
for (int i = 0; i < numScoreDrops; i++)
{
GameObject newScore = Instantiate(scoreObject, transform.position, Quaternion.Euler(new Vector3(0f, 0f, Random.Range(0f, 360f))));
}
Destroy(gameObject);
}
However, I noticed that sometimes it will drop double the number of score drops. I think this is because both collisions happen simultaneously and call die() twice. How can I avoid this race condition?
Also, the collider itself is pretty inconsistent, but I think that is unrelated to this issue. Thanks!