Quad Objects not responding to clicks.

I am very new to Unity and C#. I saw this Image Sliding Puzzle tutorial on youtube so I’ve been working on that. So, far I have instantiated the quad objects and now I want the blocks to respond to clicks. To be precise I want the block that is clicked to be exchanged with the the empty block.

I have two classes puzzle and block.

public class Puzzle : MonoBehaviour
{

public int blocksPerLine = 4;

Block emptyBlock;

void Start()
{
    CreatePuzzle();
}

void CreatePuzzle()
{
    for (int y = 0; y < blocksPerLine; y++)
    {
        for (int x = 0; x < blocksPerLine; x++)
        {
            GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad);
            blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(x, y);
            blockObject.transform.parent = transform;

            Block block = blockObject.AddComponent<Block>();
            block.OnBlockPressed += PlayerMoveBlockInput;

            if (y == 0 && x == (blocksPerLine -1))
            {
                blockObject.SetActive(false);
                emptyBlock = block;
            }
        }
    }
    Camera.main.orthographicSize = blocksPerLine * 1.5f;
}

private void PlayerMoveBlockInput(Block blockToMove)
{
    Vector2 targetPosition = emptyBlock.transform.position;
    emptyBlock.transform.position = blockToMove.transform.position;
    blockToMove.transform.position = targetPosition;
}

}

public class Block : MonoBehaviour {

public event System.Action<Block> OnBlockPressed;

private void OnMouseDown()
{
    if(OnBlockPressed != null)
    {
        OnBlockPressed(this);
    }
}

}

I even tried writing log statements to see if the click is actually working, But it won’t work as well.

Any suggestion would be really helpful :slight_smile:
I am also interested in any ideas to do this in a better and simpler way.

Make sure that on your quad:

  1. Block script is attached

  2. It is having a collider attached to it.

The script Block will take OnMouseDown trigger only when the collider is attached to the same object. Do not attach it to any child/sibling/parent respective to the collider.