Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by upariver · Jun 10, 2016 at 03:04 PM · collider2doncollisionenter

Collision2D not working

I followed a snake game tutorial and am trying to make it die if touches itself. For that I made a Tag for the new parts of the snake body as "Body" and wrote:

   void OnCollisionEnter(Collider2D coll)
     {
         if (coll.gameObject.tag == "Body")
         {
             Debug.Log("fj");
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
         }
 
     }

For some reason this does not work, however if I set the collider2d as Trigger and add it to my OnTriggerEnter function, it works like a charm.

Are there any rules for OnCollisionEnter that I am not doing right?

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 JedBeryll · Jun 10, 2016 at 04:04 PM 1
Share

I think the only rule is to have a rigidbody on it. I assume you didn't change the collision layers.

avatar image upariver JedBeryll · Jun 10, 2016 at 07:31 PM 0
Share

Yes I have that. Thanks anyway :D

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by vintar · Jun 10, 2016 at 04:02 PM

You need to use OnCollisionEnter2D

Comment
Add comment · Show 3 · 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 upariver · Jun 10, 2016 at 07:31 PM 0
Share

Still doesn't work :/, I have a rigidbody with kinematic on and gravity 0.

avatar image CirseiHyuga · Jan 25, 2017 at 10:31 PM 0
Share

OnCollisionEnter2D(Collision2D coll)...But you set Collider2D as parameter, I think it should be a problem.

avatar image CirseiHyuga · Jan 25, 2017 at 10:31 PM 0
Share

OnCollisionEnter2D(Collision2D coll)...But you set Collider2D as parameter, I think it should be a problem.

avatar image
0

Answer by yel0x · Jan 25, 2017 at 06:56 PM

Try coll.collider.gameObject.tag

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 adityapatnaik25 · Oct 19, 2019 at 06:24 AM

Uncheck the isTrigger flag on the Collider component, solved the problemfor me(if you are using OnCollisionEnter2D).

- If that didn't work for you try doing these:

  1. Change the Body type to Dynamic and not kinematic if you have a Rigidbody2D.

  2. Check the Zee position of your spawner, source object,target object. Make sure that they are on the same Zee because otherwise they wont collide obviously.

  3. Check that your script and class names match.

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

62 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 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 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

How To Decide To Damage Between Colliding Cars 4 Answers

2D Game - Get collider name 1 Answer

4 line code not working! 1 Answer

How can i store all colliders of colliders which collide with a prefab? 1 Answer

How can i get a transform to move to another transform if it is touching it? 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