if statement and child

Is there any way to use an if statement to make something a child to the player. For my reason ive got the player shooting a raycast out of the camera and i need it so that if the raycast hits a certain layer i need it to make that a child to the player.

You can get the object from the raycast and do transform.SetParent