Search code examples
unity-game-enginespritemesh-collider

Unity 2D On Collision (Do Something) not working?


I know this question has probably been asked many times, but I'm going to ask again.For some reason, my colliders won't work. I have one on my block sprite, and another on my 'miner' sprite. When I hit play, the two start on top of each other. (I'm not sure if this matters, it appears to make no difference.) Each one has a Box Collider 2D. In the script assigned to one of them, it tries to see the collision.

void OnCollisionEnter2D(Collision2D collision)
{
    if (col.gameObject.name == "Miner")
    {
        GameObject.Find("Miner").GetComponent<miner>().block = block;
    }
}

However, this script does not seem to detect the Miner sprite colliding with it. I am certain I am deriving from MonoBehavior, so it's not issue there.


Solution

  • Do either of the GameObjects have a RigidBody2D attached to them? At least one GameObject needs to have a RigidBody in any given collision.