• 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
Question by Gusic · Feb 17, 2014 at 04:15 PM · c#2dcollisiongameobjectdestroy

Simple on Collision Help (C#)

Hi Guys, I think i have a very simple problem. Basically i want a collision script. So when my player touches the spikes he dies. i have made a script but it just wont work, i am a beginner by the way. Please it would be nice if you help.

Thank You

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDie : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     
     }
     void OnCollisionEnter(Collision collision){
         if (collision.gameObject.name == "Spikes") {
             Destroy(collision.gameObject);        
         }
     }
 }
 
Comment

People who like this

0 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 Sundar · Feb 17, 2014 at 05:52 PM 0
Share

Debug to check whether collision actually happens like

 void OnCollisionEnter(Collision collision)
 {
     Debug.Log(collision.gameObject.name);
     if (collision.gameObject.name == "Spikes") {
     Destroy(collision.gameObject);
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by clunk47 · Feb 17, 2014 at 06:54 PM

Looks like you're using Rigidbody2D. For some reason w/ the 2D stuff, I can only get collisions to work correctly if both objects included have Collider2D AND Rigidbody2D. You also need to use OnCollisionEnter2D, not OnCollisionEnter.

Comment
poncho

People who like this

1 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 Gusic · Feb 17, 2014 at 07:05 PM 0
Share

YES!! it worked thank you. i just need to respawn the player now.

avatar image poncho · Feb 17, 2014 at 07:12 PM 1
Share

thanks for the heads up, i have not done anything with Unity on 2D framework, but good to know it before. Gusic, mark this question as the answer

avatar image

Answer by poncho · Feb 17, 2014 at 05:35 PM

I am not sure from this code that if the player is the one with the PlayerDie Script, or if the spikes have the script, since the collision is expecting to be against "Spikes", i will asume that the player has the script, then collission.gameobject will be the spikes, meaning that if the player collides with the spikes the object to be destroyed would be the spikes, if you would want the player to be destroyed use Destroy(gameObject); in case the script is attached to the player

Comment

People who like this

0 Show 6 · 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 Gusic · Feb 17, 2014 at 05:37 PM 0
Share

i changed it to gameObject but it still wont be destroyed(the player) i have attached the script to the player, so when the player hits the spikes he dies.

avatar image poncho · Feb 17, 2014 at 05:40 PM 0
Share

make sure both gameobjects have colliders and are not nriggers

avatar image iAmAwsum poncho · Feb 14, 2016 at 10:58 AM 0
Share

THANK YOU! I had is trigger on my obstacle and you finally fixed it! :D

avatar image Gusic · Feb 17, 2014 at 05:43 PM 0
Share

it still doesnt work the player still doesnt get destroyed.

avatar image whydoidoit · Feb 17, 2014 at 06:02 PM 0
Share

Make sure the thing with the script has a rigidbody attached. Also see this: http://unitygems.com/gotchas

avatar image Gusic · Feb 17, 2014 at 06:13 PM 0
Share

My player does have a rigid body. look http://gyazo.com/79ada36bd80c6aadeac581aeee18a3a9

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

23 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

Related Questions

destroying gameobject from other script 1 Answer

gameObject doesn't self-destruct after collision c# 2 Answers

How to only delete one of two collided objects? 1 Answer

2D C# destroy a GameObject on collision 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 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