Snap to grid

I have the following code that works, however it will not snap both X and Z but only the X or only the Z depending on what the last line of code is.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(MeshRenderer))]


public class MouseDrag : MonoBehaviour {


	
	
	Vector3
		screenPoint,
		scanPos,
		curPositionx,
		curPositionz,
		curScreenPoint;
	
	
	public float
		gridSize = 1;
	
	
	
	void Awake() {
		scanPos = gameObject.transform.position;
		screenPoint = Camera.main.WorldToScreenPoint(scanPos);

	}
	

	
	
	void Update() {


		curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
		curPositionx = Camera.main.ScreenToWorldPoint (curScreenPoint);
		curPositionz = Camera.main.ScreenToWorldPoint (curScreenPoint);

		

		curPositionx.x = (int)(Mathf.Round (curPositionx.x) * gridSize);
		curPositionz.z = (int)(Mathf.Round (curPositionz.z) * gridSize);

	
	
		

		transform.position = curPositionz;
		transform.position = curPositionx;



	}
	
	
}

If the last line is as above then it will snap on the X axis but not the Z.

If the last 2 line of code are reversed then the Z axis will snap and not the X axis.

Is there away to combine them both together? or am I looking at it the wrong way?

you are kinda going about it the wrong way

     transform.position = curPositionz;
     transform.position = curPositionx;

with these two lines of code being consecutive, unity will first assign curPositionz to transform.position, IMMEDIATELY afterwards it will assign curPositionx to transform.position

this will have the effect of only the last line of code actually making a visible change.

it seems strange that you are storing the values in the way that you are, you would be better off having

         Vector3 curPosition;

         curPosition.x = Camera.main.ScreenToWorldPoint (curScreenPoint);
         curPosition.z = Camera.main.ScreenToWorldPoint (curScreenPoint);
 
         
 
         curPosition.x = (int)(Mathf.Round (curPosition.x) * gridSize);
         curPosition.z = (int)(Mathf.Round (curPosition.z) * gridSize);

         transform.position = curPosition;

this would tidy up the code a lot. of course if you want to store x and z in different vector3’s you could change the last line of code (replacing the 2 you have now) to :

transform.position = new Vector3(curPositionx.x,transform.position.y,curPositionz.z);

this would also work

void Update() {

         curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         curPositionx = Camera.main.ScreenToWorldPoint (curScreenPoint);
 
         curPositionx.x = (int)(Mathf.Round (curPositionx.x) * gridSize);
         curPositionx.z = (int)(Mathf.Round (curPositionx.z) * gridSize);

         transform.position = curPositionx;
     }