how to distinguish which layer has been hit by raycast first?

hey guys,
so i’ve been trying to make a raycast bullet script, which a single ray could hit different game objects in different layers.
i have a gun and boxes in my scene one is stone and the other is a wooden one and when i shoot the ray cast bullet passes through all two boxes.sometimes the stone one is in front sometimes the wooden one. how can i tell the script that the first box in the front should be effected and the one in the back should not?
i created a small gif file to show my problem:
http://s6.uplod.ir/i/00920/6bcpcmir38ad.gif
and here is my code:

Ray rayshow = new Ray (transform.position, transform.forward);
RaycastHit hitinfo;


		if (Physics.Raycast (rayshow, out hitinfo, range, stoneLayers)) {

			if (hitinfo.collider != null)
			if (hitinfo.collider.gameObject.layer == LayerMask.NameToLayer ("stone")) {


				hitstone ();


			}


			if (Physics.Raycast (rayshow, out hitinfo, range, woodLayers)) {
				

				if (hitinfo.collider != null)
				if (hitinfo.collider.gameObject.layer == LayerMask.NameToLayer ("wood")) {
					
					

						
					hitwoood ();


			

				}


			}




		}
	}
	public void hitstone ()
	{


		Ray rayshow = new Ray (transform.position, transform.forward);


		Debug.DrawLine (rayshow.origin, hitinfo.point, Color.yellow);


		targetForGun targeti = hitinfo.transform.GetComponent<targetForGun> ();

		if (targeti != null) {

			targeti.takedamge (damage);

		}
		if (hitinfo.rigidbody != null) {

			hitinfo.rigidbody.AddForce (-hitinfo.normal * BulletImpactForce);

		}


		GameObject impatbullet = Instantiate (stoneHitImpact, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));

		impatbullet.transform.parent = hitinfo.transform;


		Destroy (impatbullet, 6f);
		GameObject stonesound = Instantiate (stoneHitSound, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));

		impatbullet.transform.parent = hitinfo.transform;


		Destroy (stonesound, 6f);









	}


	public void hitwoood ()
	{

		Ray rayshow = new Ray (transform.position, transform.forward);
		targetForGun targeti = hitinfo.transform.GetComponent<targetForGun> ();

		if (targeti != null) {

			targeti.takedamge (damage);

		}
		if (hitinfo.rigidbody != null) {

			hitinfo.rigidbody.AddForce (-hitinfo.normal * BulletImpactForce);

		}
		GameObject impatbullet = Instantiate (WoodHitImpact, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));

		impatbullet.transform.parent = hitinfo.transform;

		Destroy (impatbullet, 6f);





		GameObject woodsound = Instantiate (WoodHitSound, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));

		impatbullet.transform.parent = hitinfo.transform;


		Destroy (woodsound, 6f);



	}
}

@h00man
Take a layermask with 2 layers and use only 1 raycast. Thus only the object which is in front would be detected. You can put your checks for layer inside.

You can either make the layermask variable public and set in inspector or see the below link for setting in code.