Sprites/Colliders getting stuck together

Idea of the game is to put boxes into their zones. Blue boxes into blue zones, red to red, etc. The boxes and zones each have their own colliders, the boxes have a RigidBody2D, and the zones are triggers. If the zone color matches the box color then the box gets destroyed. If it is incorrect then nothing is suppose to happen. Issue is, if a box is placed in the wrong zone then it gets stuck and cannot be dragged out of the zone in the game view. I’ve tried adding different colliders to the zones and a physics material with zero friction. Maybe I am just unfamiliar with colliders and this is normal?

Please post your zone-checking/box destroying script here.

The Debug.Log in OnMouseDown was to see if it would even register a mouse click, which it does not. zoneColor just logs the color of the zone the box enters. Upon exiting the color goes to white; indicating it is not in any zone.

private void OnTriggerEnter2D(Collider2D collider)
    {
        switch (collider.gameObject.tag)
        {
            case "RedZone":
                zoneColor = collider.gameObject.GetComponent<SpriteRenderer>().color;
                Debug.Log("Entering Red Zone");
                break;
            case "BlueZone":
                zoneColor = collider.gameObject.GetComponent<SpriteRenderer>().color;
                Debug.Log("Entering Blue Zone");
                break;
            case "YellowZone":
                zoneColor = collider.gameObject.GetComponent<SpriteRenderer>().color;
                Debug.Log("Entering Yellow Zone");
                break;
            default:
                break;
        }
    }

    private void OnTriggerExit2D(Collider2D collider)
    {
        switch (collider.gameObject.tag)
        {
            case "RedZone":
                zoneColor = Color.white;
                Debug.Log("Exiting Red Zone");
                break;
            case "BlueZone":
                zoneColor = Color.white;
                Debug.Log("Exiting Blue Zone");
                break;
            case "YellowZone":
                zoneColor = Color.white;
                Debug.Log("Exiting Yellow Zone");
                break;
            default:
                break;
        }
    }

private void OnMouseDown()
    {
        hasBeenPickedUp = true;
        Debug.Log("Clicked on box");
    }
  
    private void OnMouseUp()
    {
        if (thisBoxColor == zoneColor)
        {
            Destroy(gameObject);
        }
        else Debug.Log("Box does not belong here");
    }

    private void OnMouseDrag()
    {
        Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3(newPosition.x, newPosition.y);
    }

Not sure if I see anything wrong, but does “hasBeenPickedUp” have any significance? Since you haven’t set it to false when OnMouseUp is called. Perhaps you have disabled picking objects when hasBeenPickedUp is true, in some other code?

hasBeenPickedUp is used in Update to determine if they should move to waypoints or not. If they have been picked up they don’t move towards waypoints anymore. They can still be picked up and moved around, just not when they are in the wrong zone. That’s why I was assuming it had something to do with the colliders of each object.

Golly gee do I feel silly :sweat_smile: I didn’t even think about it until I read another thread regarding order layers. While my boxes are in a higher order to draw them on above the zones, in game they are on a lesser Z axis than the zones making them unable to be interacted with. I noticed it when I went into 3D view.