Prefabs not colliding with player

I’m making a simple top-down dungeon game. I’m using prefabs to generate the dungeons but for whatever reason the prefabs don’t work with the script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ToClose : MonoBehaviour
{

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name == "Player")
        {
            print("Worked");
        }
    }
}

For testing reasons I’m just trying to get them to interact with the player and print “Worked” however it’s just not printing. This works when the script isn’t in prefabs and is just in regular sprites or tile maps so I’m not sure what to do.

Hello.

First, why your tittle is “Prefabs” not collide? This means other things are already colliding and only prefabs arent?

Second, Is the player the only thing that doesnt collide?

Third: Did you read the collision manual? I suppose no, 90% of questions in Unity answers about collisions not working is for people that dindt read the manual and did not have a rigidbody…
Spend your time, before coming here to make us to spend ours in your problem. We are gld to help, but with difficult/complex things. The basics must be learn by your own.

(Sorry if this was not your problem, give more info then… screenshots of both objects inspectors for example…)


Description
A base class of all colliders.

See Also: BoxCollider, SphereCollider, CapsuleCollider, MeshCollider, PhysicMaterial, Rigidbody.

If the object with the Collider needs to be moved during gameplay then you should also attach a Rigidbody component to the object. The Rigidbody can be set to be kinematic if you don’t want the object to have physical interaction with other objects.