- Home /
How to parent a prefab?
Im making a laser beam follow my ship after firing it by making the ship its parent. However I'm getting error: "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."
My code: var blah : Transform; function Start () { transform.parent = blah; }
The beam instantiates from ship prefab.
Thanks in advance.
Answer by BiG · Jun 21, 2013 at 11:19 AM
You are trying to assign the prefab itself, and not the object instantiated from it. Look here, this question is similar to yours, and it should help:
http://answers.unity3d.com/questions/341714/setting-the-parent-of-a-transform-which-resides-in.html
ok I this is my other code I decided to parent from that way.
var lazorObject : GameObject = Instantiate(lazor, Vector3(transform.position.x, transform.position.y + 7, 5), Quaternion.Euler(0,0,180)) as GameObject;
lazorObject.transform.parent = transform;
And now I'm getting this error: NullReferenceException: Object reference not set to an instance of an object
Answer by Quaker_SDR · Jun 21, 2013 at 11:53 AM
Once you instantiate the prefab then only you can assign the parent for that prefab. Making parent to prefab at runtime is not possible.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104606im_/https://answers.unity.com/themes/thub/images/avi.jpg)