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
3
Question by danman1218 · May 21, 2014 at 10:36 PM · c#2djavascriptios

Destroy Object On Collision?

I'm making a 2D game and need to make a block that when the player runs into it, he gets destroyed. This is my script but it isn't working. Any help?`using UnityEngine; using System.Collections;

public class Bounds : MonoBehaviour {

 void OnCollisionEnter (Collision col)
 {
     if(col.gameObject.name == "DeathCube")
     {
         Destroy(col.gameObject);
     }
 }

} `

Comment
Add comment · Show 2
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 robertbu · May 21, 2014 at 10:36 PM 0
Share

How are you moving your character? Does your character have a non-kinematic Rigidbody?

avatar image danman1218 · May 21, 2014 at 10:54 PM 0
Share

It is non-kinematic

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by JayOhhh · May 30, 2014 at 04:10 PM

     void OnCollisionEnter2D (Collision2D col)
     {
       if(col.gameObject.name == "DeathCube")
       {
        Destroy(col.gameObject);
       }
     }

First switch the method to be in 2D. Ensure that you have a 2D collider and 2D rigid body attached to both objects. Also is your DeathCube instantiated from a prefab? If it is you will need to check the actual name on your scene. Most of the times I find instatiated prefabs append the (Clone) value to the end of the name. Maybe try and putting in a debug statement to ensure you are matching the names correctly.

         void OnCollisionEnter2D (Collision2D col)
         {
           //Check collision name
           Debug.Log("collision name = " + col.gameObject.name);
           if(col.gameObject.name == "DeathCube")
           {
            Destroy(col.gameObject);
           }
         }
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 HowlinnWolf · May 10, 2017 at 11:20 AM 0
Share

Thank you! I had the same issue and changing Box Collider to Box Collider 2D fixed it!

avatar image
3

Answer by Sisso · May 21, 2014 at 11:18 PM

If you are using 2D physic you must use 2D callbacks

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html

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 danman1218 · May 21, 2014 at 11:27 PM 0
Share

Ok, what about destroying the object?

avatar image Sisso · May 22, 2014 at 04:43 PM 0
Share

Like you did, Destroy.

http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

avatar image
0

Answer by dhbzaa · Apr 17, 2019 at 03:32 AM

I am using the unity 3d I want that when my jetfighter collided with mountains or terrain the jet destroyed but my script cant work void OnCollisionEnter (Collisiion col) { if(col.gameObject.name == "Player") { Destroy(col.gameObject); } }

and I put this script in terrain and mountain but it cant 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

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

25 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Particle System On Key Press 2 Answers

Disable JIT compilation in the Editor (Enable Full-AOT mode) 0 Answers


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