Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by xtkbilly · May 26, 2012 at 09:48 PM · collisiondestroydeath

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kolban · May 26, 2012 at 10:28 PM 0
Share

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.

avatar image aldonaletto · May 26, 2012 at 10:31 PM 0
Share

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?

avatar image xtkbilly · May 28, 2012 at 08:57 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xtkbilly · May 28, 2012 at 08:46 PM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 28, 2012 at 09:06 PM 0
Share

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.

avatar image whydoidoit · May 28, 2012 at 09:08 PM 0
Share

Don't worry, everyone seems to go through this - yours truly was no exception

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kolban · May 26, 2012 at 10:26 PM 0
Share

Take care with the "if" statement in this example ... I think it should be:

if (collisioninfo.gameObject.tag == "Enemy")

avatar image Eric5h5 · May 26, 2012 at 10:34 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

health decrease only once and stop player movement 1 Answer

destroying gameobject from other script 1 Answer

Destroy(other.gameObject) isn't destroying parent, only children 2 Answers

collision script to destroy 2 Answers

Destroying object when player walks over it 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges