Accessing all Selection.activeTransforms

I wrote a script that snaps game objects to a basic grid that runs in the editor, however when i select more than one object, it doesn’t work right. I want to extend the function to cover all the objects in my selection.

I don’t think i fully understand yet how for / foreach loops work, or how data is being passed around.

Here is what i have so far (I commented out stuff that was causing problems):

void Update () {

		//Selection[] mySelection = Selection.activeTransform;

		if   (Selection.activeTransform) {

			//foreach (Selection selection in mySelection) {

		float snapX = Mathf.Round (Selection.activeTransform.transform.position.x);
		float snapY = Mathf.Round (Selection.activeTransform.transform.position.y);
		float snapZ = Mathf.Round (Selection.activeTransform.transform.position.z);

		//Debug.Log (snapX);
		//Debug.Log (snapY);
		//Debug.Log (snapZ);

		Selection.activeTransform.transform.position = new Vector3(snapX, snapY, snapZ);

			//}
		}

You can use a foreach loop on the Selection.gameObjects array, as follows…

foreach(GameObject gameObject in Selection.gameObjects)
{
	Debug.Log("I am a selected transform..." + gameObject.transform.position.ToString());
}