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);
}
}
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.
The correct function name is OnTriggerEnter2D - you could just google it and find the documentation.
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…