if ( Physics.Raycast ( ray, out hit, Mathf.Infinity, LayerMask ) ) not working?

Why isn’t it working when I put in that condition?

It works fine when I remove it, but I really need to make it so that it will only be executed when the player is touching the specific LayerMask.

EDIT:
not touching the specific LayerMask.

When I remove it, the code inside gets executed properly but it still gets executed even when I’m not touching the object with the said LayerMask.

I don’t want it to get executed when it’s not touching the LayerMask, and when I put in the condition to make it so, it doesn’t get executed at all, wherever I touch the screen.

Here’s the code I’m using…

using UnityEngine;
using System.Collections;

public class TouchMovement : MonoBehaviour {

private RaycastHit hit;

public static bool moving;

Ray ray;

int currTouch;
int touch2watch;

LayerMask layerZ;

void Start ()
	{
		layerZ = 1 << 12;
            //All layers except the 12th one...
		layerZ = ~layerZ;

		touch2watch = 64;

		moving = false;
	}

void Update ()
	{
if(Input.touchCount > 0)
		{
			for(int i = 0; i < Input.touchCount; i++)
			{

					currTouch = i;

					//If the player hits any position on the screen EXCEPT for the object with the layerZ...
					if(Physics.Raycast(ray, out hit, Mathf.Infinity, layerZ))
					{
						//Write "touched"
						Debug.Log ("touched");

					//Why is Debug.Log ("touched") not being executed? Including the ones below it...
					//When I remove the condition "if(Physics.Raycast...", it works fine.
					//But I need it so that this block of code will only be executed when the player is not hitting the object with the LayerZ.

						if(Input.GetTouch(i).phase == TouchPhase.Began && !moving)
						{
							touch2watch = currTouch;

							if(touch2watch == currTouch)
							{
								moving = true;
							}


						}

					}


				if(touch2watch == currTouch)
				{

					if(Input.GetTouch(i).phase == TouchPhase.Ended)
					{
						touch2watch = 64;
					}

					if(Input.GetTouch(i).phase == TouchPhase.Ended)
					{
						moving = false;
					}

				}


				
			}

			
		}
	}
}

Thanks!

There’s no information in your Ray

Right now you are ignoring the 12th layer. If you want to hit only the 12th layer, you need to make sure only the 12th bit of the layer mask is ON. Right now the bits from 0 to 11 are ON and the 12th bit is OFF.