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 npoguca · Nov 01, 2015 at 08:32 AM · 2dcollision detection

OnCollisionEnter2D issues in Unity 5.2

At this point I'm desperate. I just can't get that thing to work. I've got two prefabs initiated through code

 void Start()
     {
         first = Instantiate(Resources.Load("poke"), new Vector3(), Quaternion.identity) as GameObject;
         second = Instantiate(Resources.Load("poke"), new Vector3(), Quaternion.identity) as GameObject;
         first.AddComponent<Rigidbody2D>();
         second.AddComponent<Rigidbody2D>();
         first.AddComponent<CircleCollider2D>();
         second.AddComponent<CircleCollider2D>();
         first.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
         second.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
 }

Both of them now have a rigidbody and a collider. Triggers are of. kinematic property is off. Gravity is set to zero. When the objects collide, they act like they should, bumping into eachother but the event won't fire.

  void OnCollisionEnter2D(Collision2D collision)
     {
         Debug.Log("collision");
     }

I tried moving the objects by transforming their position and by adding Force to them. What is going on?

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 aditya007 · Sep 25, 2016 at 04:56 PM 0
Share

First, You should use "first.GetComponent()" ins$$anonymous$$d of "first.gameObject.GetComponent()".

For the event not firing, verify that the script is attached to the prefab you are instantiating.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SarfaraazAlladin · Sep 25, 2016 at 05:33 PM

I tried your exact code and everything worked as expected. Some assumptions I made:

  • The OnCollisionEnter2D method is in a seperate script attached to your poke prefab.

  • The thing that your poke prefab is colliding with is also a 2D collider.

If you move your objects by transforming them, the isKinematic bool gets set to true while that's happening, so you're better off adding force in this case.

Also, have you considered using OnTriggerEnter to see if that would work? There's a really good breakdown of when to use what here:

http://answers.unity3d.com/questions/875770/ontriggerenter-or-oncollisionenter-1.html

I can never keep it straight in my head though, so I just end up trying them both to see what works best for my setup.

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

45 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

Related Questions

2D collision not working 0 Answers

Bounce not calling OnCollisionEnter2D - how fast is too fast? 1 Answer

LineRenderer Collisions (that support texture alpha) 0 Answers

How would I implement collisions with my tile-based movement? 0 Answers

2D colliders doesnt work with Gravity Scale = 0 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