- Home /
My 2D collision wont work?
So I am trying to create a coin that the player can pick up and I'm pretty new to unity 2D. For some reason even though my character has a box collider and a rigid body, and the coin has a rigid body, it wont collide! Is the issue my code?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class coinpickup : MonoBehaviour {
public float coins = 0;
public GameObject coin;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTrigger2DEnter(Collider2D col)
{
coins = coins + 1;
Destroy(coin.gameObject);
}
}
Answer by heskey30 · Jun 17, 2017 at 12:00 AM
The correct function name is OnTriggerEnter2D - you could just google it and find the documentation.
Answer by zergnick · Jun 17, 2017 at 01:11 AM
Did you add a collider to the coin? Normally I will not use a rigid body for the coin if I only need to pick it up. Also, if you use OnTriggerEnter2D, you need check the 'Is Trigger' in the Coin's collider. Otherwise, you could use OnCollisionEnter2D.
assu$$anonymous$$g that you are using 2d colliders and physics
Answer by JxWolfe · Jun 17, 2017 at 01:04 AM
The box Collider has to have isTrigger set to true, and it sounds like you are using 3d Colliders and Physics... did you mean boxCollider2d and Rigidbody2d? otherwise you would use:
void OnTriggerEnter(Collider other)
{
coins=coins+1;
Destroy(coin.gameObject);
}
That would be my idea, and you should have the coins public static or on the player....
Your answer
![](https://koobas.hobune.stream/wayback/20220612124327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SOLVED] How to Destroy a rigidbody game object on collision with another gameobject? 4 Answers
IAP doesn't work in google play store (on device) but works fine in the editor. 0 Answers
Unity 2018 "Not Responding" in task manager when I try to make a build. 0 Answers
2d racing play button not working 2 Answers