Sticking, Dragging, and Unsticking Objects

I’m currently working on a game where the user can drag objects around and make them stick together. To do this, I have two objects: the object currently being dragged, and the object that it collides with. Upon collision, the object being collided with sets its parent to the object being dragged, and vice versa.

The idea is that the player can then click on either of those objects and drag them; both objects will move as one, any objects collided with will then join the heap, and they’ll all become, effectively, one super object.

The issues I’m having are as follows:
1). If we drag Object A into Object B, B will stick with A and the two will be dragged along as one object. If, however, we start dragging Object B, that one will move on its own, without A. To make matters even weirder, if we then start dragging A again, B will then move along with A, although they aren’t physically attached any more.

My “attaching” code is as follows:

void OnCollisionEnter(Collision collision)
	{
		if (beingDragged == false) 
		{
			GameObject otherObject = collision.gameObject;
			Ball otherObjectScript = otherObject.GetComponent<Ball>();
			
			//only stick to an object we're currently dragging
			if (otherObjectScript.isBeingDragged() == true)
			{

				gameObject.transform.parent = otherObject.transform;
				otherObject.transform.parent = gameObject.transform;
			}
			
		}
	}

2). I’m looking to implement a hazard that will disconnect some of the objects from the main stack – not all of them, but some of them. My first instinct is simply set the parents of the afflicted objects to null, but how can I tell the main object they aren’t connected any more? Since there’s more than one object attached to it, I don’t think I can simply say that mainObject.parent = null.

Is there a better way to go about doing all of this?

Any help would be sincerely appreciated.

Did you read How could I make it so that two objects snap to eachother? - Questions & Answers - Unity Discussions ?