A way to attach a GameObject to another?

I’m trying to attach a game object to another GO through script where it will stay in the same hierarchy and animate with what it is attached to, like a gun attached to a rig.

Is there anyway to do this in code? I’ve been able to get specific nodes on my rig but can’t seem to find a function that allows me to do the above, all i could find was GameObject.AddComponent()

Any ideas?

if “gun” is your gun gameobject, and “rig” is your rig gameobject:

gun.transform.parent=rig.transform;

Same as dragging and dropping the gun inside the rig in the heirarchy.

That easy. Thank you very much.