- Home /
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();
}
}
First thing to do is Debug.Log it, chances are one or more of your coins is tagged Enemy.
Thank you for your reply. There is only one coin as prefab tagged coin. The script is always instantiating this coin usin Random for random time and position. If any of the coins was tagged "enemy" only by mistake, there would be sound and explosion but non of them are shown. I thing this is some kind of Unity bug...
I really dont understand to Debug.Log("Script Name : Function Name : Tag Name") - where should I put it?
I put this into the OnTriggerEnter function: Debug.Log(other.gameObject). Everything the player touches it is written in the console. But when the plyer suddenly dissapears it says nothing. The player dissapears just when it hits a coin but at this point it even does not print that the player touched this coin that made him dissapear...
I searched for destoy - there is only one more. The whole game is in boundary that destroys evering that comes out of it:
public class DestroyByBoundary : $$anonymous$$onoBehaviour {
void OnTriggerExit (Collider other)
{
Destroy (other.gameObject);
}
}
Not a Unity bug, as said prior. The only chance of this happening is that there is a coin that's tagged as an enemy. Note that the explosion is instantiated at the position of the rigidbody, ins$$anonymous$$d of the transform. If so happens the coin doesn't have a rigibody, Unity throws a nullreferenceException, and possibly also animates the explosion at 0,0,0 coordinates in worldspace.
Add your debug.log within the enemy if statement.
That code is also part of the space shooter tutorial, it's used to stop bullets from hanging around once they get to the edge of the screen, it should stick with the camera but that doesn't mean yours does. I agree with @hexagonius it's the most likely candidate for the cause rather than a Unity bug. Are you sure you don't need it for something else though?
EDIT
Put the
Debug.Log(other.name + " was DestroyedByBoundary at " + Time.time);
in the OnTriggerExit, and if it was that that line will appear in the console. If the Time.time matches how long the game was running before the player vanished and more importantly if the Player name appears then that's what caused it.
Answer by Mmmpies · Feb 20, 2015 at 02:51 PM
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.
Your answer
Follow this Question
Related Questions
Simple on Collision Help (C#) 2 Answers
Removing an object if it spawns in a building 1 Answer
gameObject doesn't self-destruct after collision c# 2 Answers
Multiple Cars not working 1 Answer
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer