Raycast can't detect objects on layermask

I have two main layers being used in this particular part of my code - Player and StaticWorld.

During play, I try to run a raycast from the player to a location, and see if there’s anything that would collide with that raycast in the StaticWorld layer.

If I use

RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position, clickDirection, clickDistance);
if (hit)
{
	Debug.Log ("Hit on layer " + hit.transform.gameObject.layer + ", Target layer: " + LayerMask.NameToLayer("StaticWorld"));
}

Then when it hits objects in the StaticWorld layer, I get the message

“Hit on layer 8, Target layer: 8”.

If I try to use

RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position, clickDirection, clickDistance, LayerMask.NameToLayer("StaticWorld"));
if (hit)
{
	Debug.Log ("hit on layer " + hit.transform.gameObject.layer + " Target layer: " + LayerMask.NameToLayer("StaticWorld"));
}

then I cease to get any raycast hits on the same objects from the same location. I’ve also tried using the static value 8 for the layermask with the same results. Am I missing something here?

Are you sure LayerMask.NameToLayer(“StaticWorld”) is correct?
If you layer is with index 8, try this.

int mylayer = 1 << 8;

if(Physics2D.Raycast(gameObject.transform.position, clickDirection, clickDistance, mylayer){
    Debug.Log ("hit");
}

To see what and where you are casting you can use ray debug.

Debug.DrawRay(gameObject.transform.position, clickDirection, Color.red);