- Home /
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");
}
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).
But, where is the script containing the OnCollisionEnter2D ? It must be inside of one of the objects.
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!
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
Your answer
Follow this Question
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