My player is suddenly and rarely destroyd when collecting coins

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();
		}
	}

OK then that’s unlikely but do Debug.Log(“Script Name : Function Name : Tag Name”), well I don’t know your script name but “MyScriptName : OnTriggerEnter : Enemy” and another one for Coin.

Assuming you’re using Mono click Search → Find in Files and search for Destroy and debug any others that you find and see if it debugs what kills your player.

Also run the game without Miximize so you can flick to the Editor and see if your player has vanished whilst the game’s running.

EDIT

Anywhere you have a Destroy command put a Debug.Log just above it that shows the script/function and what test has been done (Enemy/Coin). All it does is output to the console like an error message, the last one that appears when your player disappears should be what’s killing the player (if the player is being killed). It’s just diagnostics to see if we can narrow down what’s happening.

EDIT

OK I converted this to the answer as it best shows to Debug that was needed to find the issue but I’ll add the actual Debug line that proved it:

Debug.Log(other.name + " was DestroyedByBoundary at " + Time.time);

Glad you got it sorted.