- Home /
Question by
CB-TV · Jan 30, 2014 at 09:15 PM ·
parentchildmain cameratransform.
Parent Game Object to Camera?
I want to parent the game object the script is attached to to the main camera. I have this so far:
// Get the transform of the camera
var camera = Camera.main.transform;
// make it a child of the current object
camera.parent = transform;
// place it behind the current object
camera.localPosition = -Vector3.forward * 5;
I know this script does the complete opposite but I don't know what to do from here to parent the game object that the script is attached to to the main camera. Do you know?
Comment
Best Answer
Answer by CB-TV · Jan 30, 2014 at 09:22 PM
// Get the transform of the camera
var object = gameObject.transform;
// make it a child of the current object
object.parent = Camera.main.transform;
// place it behind the current object
object.localPosition = -Vector3.forward * 5;
Answer by SentientTuba · Jan 30, 2014 at 10:12 PM
you could make a public variable and put the gameobject in in the inspector, then try
gameobject.transform.parent = Camera.main.transform;
and see if that works?