How to set instantiated objects as children of another instantiated object?

Ok, so I’m trying to solve this :

objects have a boolean called “selected”
right clicking on the objects toggles this on and off.

When I press ‘B’, I want an empty gameobject to be Instantiated and set as the parent of all selected objects.
I can then select more objects, press ‘B’, creating another group, and so on.

I’ve been working on this for hours and getting nothing but headaches

I assume you already have a method to keep track of the selected objects and you have a reference to the object that is to become the parent. To parent one object to another, use something like this:

GameObject childObj;   // Set this to the child you want to give a home
GameObject parentObj;  // Set this to the parent

childObj.transform.parent = parentObj.transform.parent;