How to set an object to be a child of another using c#

I have a game that includes an elevator, but when the player gets on the elevator, and the animation triggers, it is very glitchy and the player is sometimes shot off the elevator. I want to make the player a child of the elevator once it touches it so it can go up and down smoothly. All help appreciated !!

1 Answer

1

You can do:

public GameObject child;
public GameObject parent;
//assign these in the inspector

void SetParent () {

child.transform.parent.SetParent(parent);

//that should work

}