How do you make an object a child of another one using a script?

How would you make an object a child of a GameObject using a script, for example

void Start () {
      this.transform.child GameObject "MainCamera"
}

or something like that.

In C#:

// To set main camera as parent
void Start()
{
    this.transform.SetParent(Camera.main.transform);
}

// To set passed-in object as parent
void SetAsMyParent(GameObject parentObject)
{
    this.transform.SetParent(parentObject.transform);
}

thanks, but how would you make it so it does not have a parent anymore?

Like this:

void RemoveParent()
{
    this.transform.SetParent(null);
}
1 Like