How to programmatically nest objects?

Let’s say there’s a case where when one object hits another they get nested, can I make a script that does that?

Transform.parent