How to get distance from raycast.

I updated my code, and the mousePos.z is still passed as 0.0. The raycast is working, as I already got results from the log. But this still isn’t working.

function Update () {
      
      //RayCast
	  var hit:RaycastHit;
      var fwd = transform.TransformDirection (Vector3.forward);
	  //var mousePos = Input.mousePosition;
	  //var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
      
      
      
      	if (Physics.Raycast (transform.position, fwd, 15)) {
			var distance = hit.distance;
		}
      
      
      
      
      //Grass Block
      if(BlockID == 0){
      
	    if (Input.GetButtonDown ("Fire1")) {
	        
	        //var fwd = transform.TransformDirection (Vector3.forward);
	        
	        var mousePos = Input.mousePosition;
	       
	        //mousePos.z = Physics.Raycast (transform.position, fwd, hit.distance);
	        //mousePos.z = 3;
	 		mousePos.z = distance;
	 
	        var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
	        Instantiate(GrassBlock, objectPos, Quaternion.identity);
		}
	}

The code as it stands will not return the results you are after, because you are not passing in the RaycastHit parameter to your function. You need to alter your code…

if (Physics.Raycast(transform.position, fwd, out hit, 15f))