Attaching objects by Scripts

Hi, I’m having trouble figuring out a script for making to different objects that attach to eachouther, I guess I’d need them to parent eachother but I’m unsure, can anyone help me please?

This is the approach I’ve taken.

function OnCollisionEnter(collision : Collision) {
var contact = collision.contacts[0];
var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
var pos = contact.point;

function Update () {

gameObject.tag = "Object";
//If the object collided with has the tag "Object" then attach to it.
//Then the attach code

All transforms have a parent property you can set in code. See docs:

This corresponds to drag’n’drop parenting in the editor.