Can someone fix my build script like minecraft please?

i am makeing a game where u design your own city but with a minecraft style to it with blocks and i have a the place and destroy script but it dosent always place it where i want. e.g if i click the side of a block to place it sometimes it goes on top and somtimes it places it in side the block i want it to place next to. so could some one please fix it so that i can place blocks on the side of blocks and so that i carnt place them in side each other please. here is my script:

using UnityEngine;
using System.Collections;

public class BuildScript : MonoBehaviour {
	
	public Transform prefab1;
    public Transform prefab2;
    public Transform prefab3;
    public Transform prefab4;
	public Transform prefab5;
    public Transform prefab6;
	public Transform prefab7;
	public Transform prefab8;
	public Transform prefab9;
    public Transform currentPrefab;
	RaycastHit hit;
	
    void Awake() {
    currentPrefab = prefab1;
}
	// Update is called once per frame
	void Update () {
	
		
		Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.5f));
		
		Vector3 G = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));
	    
		   if (Input.GetKeyDown(KeyCode.Alpha1))
       {
                  currentPrefab = prefab1; 
			      print("Grass");
       }
           if (Input.GetKeyDown(KeyCode.Alpha2))
       {
                  currentPrefab = prefab2;  
			      print("Mud");
       }
           if (Input.GetKeyDown(KeyCode.Alpha3))
       {
                  currentPrefab = prefab3; 
			      print("Sand");
       }
		  if (Input.GetKeyDown(KeyCode.Alpha4))
       {
                  currentPrefab = prefab4;   
			      print("Sandstone");
       }
		if (Input.GetKeyDown(KeyCode.Alpha5))
       {
                  currentPrefab = prefab5;
			      print("Stone");
       }
		if (Input.GetKeyDown(KeyCode.Alpha6))
       {
                  currentPrefab = prefab6;  
			      print("Cobblestone");
       }
		if (Input.GetKeyDown(KeyCode.Alpha7))
       {
                  currentPrefab = prefab7; 
			      print ("Logs");
	   }	
		if (Input.GetKeyDown(KeyCode.Alpha8))
       {
                  currentPrefab = prefab8;  
			      print("Leaves");
       }
		if (Input.GetKeyDown(KeyCode.Alpha9))
       {
                  currentPrefab = prefab9;  
			      print("Wood");
       }
           if(Input.GetMouseButtonDown(1)){
          Instantiate(currentPrefab, G, Quaternion.identity);
       }
		
		if(Physics.Raycast(ray, out hit)){
		
			if(Input.GetMouseButtonDown(0)){
				Destroy(hit.collider.gameObject);
			}
		
		
			
	}
		
}
}

If you have the grid working correctly (it places like, X “1”, Y “3”, Z “10”), you might just simply need to put it in the Main Camera. I am still new to creating games like Minecraft especially but I do know that if you do it in that fashion it works, I did it like that…