Make an instantiated object match the slope of the terrain

I’m trying to get my instantiated building to not clip through the terrain when I build on a slight slope. I’ve managed to get the building to drop to the terrain y coordinate but the x and z rotations of the building are not consistent with the gradient of the slope and so the building cuts through the terrain.

I’ve been researching this for about two hours and I’m finding it really hard to understand the posts about raycast normal. Can someone please explain how to do this in very simple terms? Here is the script:

void Update()
	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	RaycastHit hitInfo; 

	if(currentlySelectedBuilding != null)
		if(Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
			if(hitInfo.collider.tag != null && hitInfo.collider.tag == "Ground")
				Vector3 groundPosition = hitInfo.point;

				//I'm rounding the coordinates so it's easier to place buildings in perfectly straight lines etc. 
				int x = Mathf.FloorToInt(hitInfo.point.x);
				int z = Mathf.FloorToInt(hitInfo.point.z); 
				roundedMousePosition.x = x;
				roundedMousePosition.z = z; 

				//this allows me to place buildings on the same level as the slope
				roundedMousePosition.y = groundPosition.y; 

				//I need some code here to change the rotation of currentlySelectedBuidling so matches the slope:

				//make the gameObject equal to the mouse position so I can move it around the map
				currentlySelectedBuilding.transform.position = roundedMousePosition; 

				//this script detects when the building is hovering over another object that should be built on. 
				oCD = currentlySelectedBuilding.GetComponent<ObjectCollisionDetecter>(); 
				//rotate building while it's hovering

				if(Input.GetMouseButtonDown(0) && oCD.objectBelow == false )
					//sets the building at the location it was at when mouse0 was hit. 
					currentlySelectedBuilding.transform.position = roundedMousePosition; 

					//I had to make the prefab ignore raycast hit while it was moving otherwise it interacts with the raycast and creates weird artefacts. 
					//since I now want the ray to detect it, I changed it back to obstacle. 
					currentlySelectedBuilding.layer = LayerMask.NameToLayer("Obstacle");

					//this gets around the building(clone) problem. = buildingName;

					//update graph so units avoid building; 

					//make the building and ocd script null. 
					currentlySelectedBuilding = null; 
					oCD = null; 
				else if (Input.GetMouseButtonDown(0) && oCD.objectBelow == true)
					Debug.Log("You must build somewhere else!"); 

You can do something like this:

function SpawnObject(prefab : GameObject, worldPos : Vector3) {
	var hit : RaycastHit;
	if (Physics.Raycast(worldPos, Vector3.down, hit)) {
		var go = Instantiate(prefab, hit.point, Quaternion.identity);
		go.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * go.transform.rotation;

‘worldPos’ should be a coordinate that has the right x and z positions, and a ‘y’ position guaranteed to be above the surface of the terrain. The raycast returns a normal (perpendicular) vector to the surface. The Quaternion.FromToRotation() will rotate the prefab to match the angle.

P.S. For future questions, please post your script. It gives us information like what language you are coding in, and it allows us to tailor the answer to your code.