Attaching a game object as a child during runtime?

//Put the portal outside of the screen by attaching it to the camera as a chile outside the camera view
transform.position = Camera.main.transform.position;


I’m trying to attach a GameObject to a parent prefab Main Camera during runtime. However, I also wanted it to be offset outside of the screen. The code above works at the Main Camera’s position but how do I make it offset outside the screen?


transform.position = Camera.main.transform.TransformPoint(-Vector3.forward * offsetAmount);

Where offsetAmount is the distance behind the camera you want it to be.

you code doesn’t attach your portal to the camera but instead just copy the camera position into your portal object position…

I guess want you want is to set the parent of your portal transform to be your camera transform

check that :

var offset : Vector3 = 3, 0, 0;
var wanttobeparent : GameObject;

function Update()
     transform.parent = wanttobeparent.transform;
    transform.postion = offset;