HelpMeGame
HelpMeGame

Reputation: 87

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.

Upvotes: 0

Views: 2305

Answers (2)

Seth Setse
Seth Setse

Reputation: 345

Add some debug statements in your code so that you are sure this method is not running. You should read up here about Collisions in unity. Decide if these objects will be moving and set the appropriate collider. As stated above you will typically need a rigidbody.

Upvotes: 0

MSvenTorjusC1999
MSvenTorjusC1999

Reputation: 66

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

Upvotes: 3

Related Questions