• 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
0
Question by LeathalNinja1986 · Nov 18, 2011 at 11:41 PM · boxcollider

Emeny death with box collider

I have built a setup that is supposed to kill the enemy when player jumps on their head, much like a goomba from mario. but the enemy doesn't die when player jumps on the box collider, that is parented to the enemy. Any tips that might help make this work after parent var is named in the inspector?

pragma strict

var Parent : GameObject;

function OnCollision (collider : Collider){

 Destroy(Parent);
 Destroy(gameObject);

}

Comment
Add comment · Show 1
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 FLASHDENMARK · Nov 19, 2011 at 07:16 PM 0
Share

When feeding a function parameters it is best not to use predefined names $$anonymous$$g collider, gameObject, transform, ect. You wrote "collider" which the engine might mistake as "the collider of your gameObject" ins$$anonymous$$d use words like "hit" or "other" and so on, words that the engine cannot misinterpretate.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FLASHDENMARK · Nov 19, 2011 at 07:09 PM

 var Parent : GameObject;
 
 function OnCollisionEnter (){
     Destroy(Parent);
     Destroy(gameObject);
 }

There are a few things wrong with your code:

  • OnCollision is not a predefined function and it will not work in your case. Use OnCollisionEnter instead

  • There is no reason to use pragma strict(but that does no difference anyway)

  • And do not feed the function parameters when you are not using them it is just adding extra calculation, you don't want that

The above code should work.

Comment
Add comment · 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
0

Answer by mohanrao164 · Nov 19, 2011 at 08:44 AM

try this

function OnCollision (collider : Collider)
{
collider.Destroy(Parent);
collider.Destroy(gameObject);
}

Comment
Add comment · 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
0

Answer by henry96 · Nov 19, 2011 at 03:45 PM

I have heard of OnCollisionEnter , Exit, and Stay. But never heard of OnCollision. Maybe I missed that one. Anyway, in your case, I recommend you use function OnCollisionEnter ( ) . Hope this helps.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Box Collider independence from object rotation 1 Answer

Collider Displacement Issue 0 Answers

Collider closestPointOnBounds returning point that is not touching the collider 2 Answers

How to stop my object from going through another object 2 Answers

Car stops turning with a box collider 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