Hi everyone,
Is there a way how to find out what or who destroyed my player? I suggest to use function OnDestroy() and print but what? I prefer C#.
My problem is that I got a 2D scroling car game (using 3D platform). Actualy player stays and moves only on the X axis and everything else is moving towards the player using Random for creating new opponent cars and coins. The player collects coins for more points. Player has BoxCollider - trigger and coins are prefabs with BoxCollider - trigger too. Everything works fine but sometimes when the player collect the coin, player and the coin are suddenly destroyed! I have no idea why. It is strange - game is normaly working but sometimes the player disappears, not always only rarely.
Here is the key code but when the player is suddenly destroyed (removed from the hierarchy) there is no sound and no explosion:
void OnTriggerEnter(Collider other)
{
if(other.tag == "Enemy")
{
Instantiate (explosion, rigidbody.position, Quaternion.identity);
gameController.GameOver ();
Destroy (other.gameObject);
Destroy (gameObject);
}
if (other.tag == "Coin")
{
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
audio.Play();
}
}