How do i parent a GameObject to another GameObject while colliding and unparent while not colliding? (C#)

I can’t, for some reason get it working, i am relatively new to coding. I do require any help i can get.

Code:

using UnityEngine;

public class Attach : MonoBehaviour
{

GameObject mum = null;

private string objectTag;
private GameObject objectName;

[SerializeField]
Camera fpscam;

void update()
{
    if (objectTag == "vehicle")
    {
        mum = objectName;
    }

    if (objectTag != "vehicle")
    {
        mum = null;
    }

    if (mum == null)
    { 
        transform.parent = null;
    }

    if (mum != null)
    {
        transform.parent = mum.transform;
    }

}

void OnCollisionEnter(Collision col)
{
    objectTag = col.gameObject.tag;
    objectName = col.gameObject;
}

void OnCollisionExit(Collision ncol)
{
    objectTag = null;
    objectName = null;
}

}

Use transform.SetParent() instead transform.parent.