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 /
avatar image
0
Question by rustyruss1985 · May 12, 2018 at 01:28 PM · physics2dcollider2dpolygoncollider2d

objects collide but OnCollisionEnter2D not called

i'm trying to process events when two objects collide. everything seems to be working well from the physics point of view (the objects collide, and bounce off each other in a physically realistic way) but the problem is the OnCollisionEnter2D function never gets called.

anyone have any ideas? thanks

     void Start()
     {
         Camera cam = Camera.main;
         cam.orthographicSize += 6;
 
         Physics2D.gravity = Vector2.zero;
         Physics2D.velocityThreshold = 0.01f;
 
         PhysicsMaterial2D physicsMaterial = new PhysicsMaterial2D();
         physicsMaterial.bounciness = 1;
 
         Texture2D tex = Resources.Load<Texture2D>("msword") as Texture2D;
         Sprite sprite = new Sprite();
         sprite = Sprite.Create(tex, new Rect(0, 0,  285, 464), new Vector2(0f, 1f), 100);
         GameObject squareObject = new GameObject();
         squareObject.AddComponent<Rigidbody2D>();
         squareObject.AddComponent<SpriteRenderer>();
 
         SpriteRenderer SR = squareObject.GetComponent<SpriteRenderer>();
         SR.sprite = sprite;
         squareObject.AddComponent<PolygonCollider2D>();
         squareObject.GetComponent<PolygonCollider2D>().sharedMaterial = physicsMaterial;
 
         circSprite = Resources.Load("circle", typeof(Sprite)) as Sprite;
    
         GameObject circObject = new GameObject("circ1");
         SpriteRenderer rend1 = circObject.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
         rend1.sprite = circSprite;
         circObject.transform.position = new Vector2(-1, 0);
         circObject.AddComponent<Rigidbody2D>();
         circObject.GetComponent<Rigidbody2D>().velocity = new Vector2(1f, 0f);
         circObject.AddComponent<PolygonCollider2D>();
         circObject.GetComponent<PolygonCollider2D>().sharedMaterial = physicsMaterial;
         
     }
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         Debug.Log("collided"); 
     }
Comment
Add comment · Show 3
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 rustyruss1985 · May 12, 2018 at 04:17 PM 0
Share

alt text

sure! here you go

objects.png (74.6 kB)
avatar image rustyruss1985 rustyruss1985 · May 12, 2018 at 04:19 PM 0
Share

whoops, just realized i posted the same object. but their properties are virtually identical anyhow, as you can see from the code (i initialize both in the exact same way).

avatar image tormentoarmagedoom rustyruss1985 · May 12, 2018 at 04:50 PM 0
Share

But, where is the script containing the OnCollisionEnter2D ? It must be inside of one of the objects.

1 Reply

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

Answer by tormentoarmagedoom · May 12, 2018 at 03:46 PM

Good day.

Is possibible you don't have the script containing the OnCollisionEnter2D inside one of the objects? It must.


Imagine we have ObjectA and ObjectB each with a collider and at least one of them with riggidbody component.

We will say ObjecA have a rigidbody, and a component script with OnCollisionEnter()

When a another collider, like collider of ObjectB enters inside collider ObcjectA, the function will be called, in the script of ObjectA.

If the collider of objectA is marked "As trigger", you need to use OnTriggerEnter() instead.

Byee!

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 rustyruss1985 · May 12, 2018 at 07:22 PM 0
Share

i don't. i create the objects at runtime. i thought the OnCollisionEnter2D function was called for every collision that happened in the game (regardless of which object) and you then found the objects based on the Collision2D argument.

so you're saying i have to create a separate script with OnCollisionEnter2D function, and then attach it to my objects? is there no way to assign a Collision function to an object without creating a separate script? thanks

avatar image tormentoarmagedoom rustyruss1985 · May 12, 2018 at 08:15 PM 0
Share

extended answer :D

avatar image rustyruss1985 tormentoarmagedoom · May 12, 2018 at 08:45 PM 0
Share

thanks. its working now.

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

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

After Collision Matrix change in runtime, effects not immediatly visible 1 Answer

Physics2D.OverlapBoxNonAlloc not return all overlapped collider 1 Answer

Strange collisions at the edges of boxCollider2D 0 Answers

Collision with instantiated non-moving objects. 1 Answer

Is Overlap Circle Supposed to return inactive colliders? 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