- Home /
Why isn't my Death Script Working?
I'm having trouble figuring out why my code isn't working to kill my player. Here is my code:
var deathClip : AudioClip;
function OnCollisionEnter(collisioninfo : Collision) {
if (collisioninfo.gameObject.CompareTag("Enemy")) {
this.audio.Stop();
audio.PlayOneShot(deathClip);
Destroy(this.gameObject);
}
}
This is simply saved under "DeathScript", and it is placed on the player. The enemies are tagged as "Enemy" and continually follow the player. Both the player and the enemies have the BoxCollider component enabled. There is also one other script on the player, "Movement", if that may be a problem. Any insight into the problem would be great.
When debugging scripts, remember to use the "print" statement to add debug lines. I would add one in the OnCollisionEnter body and one in the enclosed "if" statement ... this will tell you if either of them are being reached.
What are the player and the enemies? Both are rigidbodies? Or both are CharacterCOntrollers? Or the player is a CharacterController and the enemies are rigidbodies? The player is destroyed when the enemy hits it, or also when the player touches the enemy?
The player and enemies were neither actually. They did not have either component on them when the script didn't work. After adding the RigidBody component though, the script did work.
The player is supposed to be destroyed when the enemy touches it. That's how I have it set for right now, though I do plan to change it in the future. I was just doing some tests to get a better understanding of how Collisions/Colliders work.
Answer by Eric5h5 · May 26, 2012 at 10:35 PM
Your script is fine; either it's not registering a collision at all, or there is no "Enemy" tag. Do some basic debugging like putting
print (collisioninfo.gameObject.name)
as the first line in the OnCollisionEnter function, just to see what, if anything, is happening.
You are right on the first part. I also tried debugging the function by putting a Debug.Log() statement in there, AND using an else statement. If the function was called, one of the two messages would have played. None of the messages played, so that meant that the function was never being called.
Answer by xtkbilly · May 28, 2012 at 08:53 PM
I believe I have fixed the problem now. The function was never being called, as it never sensed a collision (I'm not sure why though). I made both the player object and the enemies all have the RigidBody component, which then made the code work.
So, to clarify, the function was never called when both objects had the Collider Component alone, and worked after adding a RigidBody component. If anyone could explain to me why, that would be awesome.
Read the docs - that's what it says happens :) You need a physics component in there to make it work. Otherwise they aren't participating in the collision scan.
Don't worry, everyone seems to go through this - yours truly was no exception
Answer by You! · May 26, 2012 at 10:05 PM
I have never seen 'CompareTag' used in a collision conditional statement and I haven't seen 'this.gameObject' used. Your problem might be that A) the if statement is not receiving a boolean (true/false), or it does not recognize 'this.gameObject'. I would change the if statement to this:
if (collisioninfo.gameObject.tag == "Enemy")
I would also change 'this.gameObject' to just 'gameObject'.
Also check to see that the "Enemy" tag exists on the enemies. This makes a world of difference.
Take care with the "if" statement in this example ... I think it should be:
if (collisioninfo.gameObject.tag == "Enemy")
You shouldn't do it that way; you should use CompareTag, which is faster than string comparisons and doesn't allocate garbage. "this.gameObject" works the same as "gameObject"; it's pointless to use "this." like that (and I would remove it just because it doesn't add anything useful) but it doesn't actually hurt anything either.