Align to grid in runtime problem

I’m trying to create a building system following a tutorial, the object is only positioned in the first row.

Demo of the problem

Tutorial i’m using

Align code

    public void StartPreview()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 10, rayCastLayers))
        {
            if (hit.transform != this.transform)
            {
                ShowPreview(hit);
            }
        }
    }

    public void ShowPreview(RaycastHit hit2)
    {
        currentPosition = hit2.point;
        currentPosition -= Vector3.one * currentObject.size;
        currentPosition /= currentObject.size;
        currentPosition = new Vector3(Mathf.Round(currentPosition.x), Mathf.Round(currentPosition.y), Mathf.Round(currentPosition.z));
        currentPosition *= currentObject.size;
        currentPosition += Vector3.one * currentObject.size;
        currentPreviewTransform.position = currentPosition;
}

I am guessing that possibly your camera may be too close and and clipping the display of the top cubes?

anyways seems you have much more in the code than what you need i cant figure out why you have all those extra calculations. All you should need for placement is the object’s transform position and simply add the height. Thats all!!!

also your raycast is finding the cube that the mouse is hitting. i think what you really want is the highest cube of the stack you are pointing at. so i reccomend a secondary raycast to find the highest cube of the stack. i wrote this to recreate your problem. Run this in a new empty scene:

	int i;
	int i2;
	RaycastHit hit;
	RaycastHit hit2;
	GameObject preview;
	GameObject g;
	Vector3 v;
	public void Start(){
		// create a floor for example purpose;
		preview = GameObject.CreatePrimitive(PrimitiveType.Cube);
		preview.collider.enabled = false;
		preview.renderer.material.color = Color.red;
        i = 20;
		while(i>0){i--;
			i2=20;
			while(i2>0){i2--;
				g = GameObject.CreatePrimitive(PrimitiveType.Cube);
				g.transform.position=new Vector3(i,0,i2);
                g.transform.renderer.material.color = Color.green;
			}}
		Camera.main.transform.position = new Vector3 (9, 8, 0);
		Camera.main.transform.eulerAngles = new Vector3 (52, 0, 0);
	}
	public void Update()
	{           Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
				if (Physics.Raycast (ray, out hit, 100)) {
						v=hit.transform.position + new Vector3(0,100,0);
		 // second raycast to find the top cube
			            Physics.Raycast(v,-Vector3.up,out hit);
                   // this next one line is all you need to calculate your position!!!!!!
			            preview.transform.position=hit.transform.position + new Vector3 (0, hit.transform.localScale.y, 0);
		    if (Input.GetMouseButtonDown (0)) {
			g = GameObject.CreatePrimitive(PrimitiveType.Cube);
			g.transform.position=preview.transform.position;
            g.renderer.material.color = Color.blue;
			}}}