spawning object as child of other:error

i hav an enemy which needs to be spawned as the child of the main camera.the script which i am using makes the object a child of the transform . but when i pull the object from the hiehrachy window to create a preftab,it addreses the parent field as none.please help me with this error.i need the preftab to spawn as a child of the main camera.

#pragma strict


var parent: Transform;

function Start () {

}

function Update()
{

transform.parent = parent; 

}

parent is reserved word dont use it for a varaible name and dont set parent in Update because it waill happen each frame set in start instead

 var p: Transform;
 
 function Update()
 {
 
  gameObject.transform.parent = p; 
 
 }

Prefabs in the project folder can’t have any references to things in your scene. This is bacause you can have more then one scene, so if you then try to instantiate your prefab in another scene, the reference would be broken.

Instead, you should have a script added to a object in your scene, and let that have a reference to your prefab. Then you can just set the parent of the instantiated clone of your prefab.

As an example, this could be a script added to your Camera, that spawns your prefab every time you click with your left mouse button and then sets the parent.

#pragma strict

var myPrefab : Transform;
 
function Update ()
{
    if (Input.GetButtonDown("Fire1"))
    {
        var prefabClone = Instantiate(myPrefab);
        prefabClone.parent = transform;
    }
}