- Home /
Can I stop the prefab connection of a gameobject during play?
Ok I'm making a FPS and I'm trying to make a script that will allow the player to pick up weapons off the floor. I do this by instantiating the weapon in front of the player and parenting the weapon to the player. Every thing work's fine but when I made the weapon a prefab it didn't work and an error message came up and it said something like "parenting a prefab is disabled to prevent data corruption". So my solution is to break the connection of the prefab in the awake function? But I don't know how to stop the prefab connection. Anything will help thanks!
var weaponHoldPoint : Transform;
var pistol : Transform;
var assualt : Transform;
function Start ()
{
Instantiate(pistol, weaponHoldPoint.position, transform.rotation);
pistol.transform.parent = transform;
}
this should not be necessary. You can totally instantiate a prefab and bechild it to another object. I would look to fix the roof of the problem ins$$anonymous$$d of hot fixing it.
Lets see a screen shot of your setup or show some code. I am wondering if you are instantiating a child of a prefab ins$$anonymous$$d of the full prefab.
This error usually come up when you try something like that at edit mode or when you try to parent the actual prefab to something (not an instance). At runtime there is no prefab connection... Prefavs are an Editor feature.
At runtime there are only two types of objects:
scene objects (that are all objects in the current scene)
off scene objects (that are objects that are loaded, but aren't visible since they aren't in the scene. That objects used to be prefabs in the editor. You can use them as cloning source and instantiate a clone into the scene, but the object itself can't be parented to something since it doesn't belong to the scene)
I suggest you just add the script snippet you're using to your question (just edit your question).
$$anonymous$$y guess is you either try to do something at edit-time, or you work with the prefab reference directly ins$$anonymous$$d of the cloned object.
Should I add "pistol.transform.parent = transform" part into the awake function of the pistol?
Answer by Bunny83 · Feb 13, 2012 at 10:32 PM
Like i thought you are not using the clone. You try to parent the prefab directly!
Instantiate() returns the cloned object. prefab is just the source object. You should do it like this:
var weaponHoldPoint : Transform; var pistol : Transform; var assualt : Transform;
function Start () { var clone : Transform = Instantiate(pistol, weaponHoldPoint.position, transform.rotation); clone.parent = transform; }
You should take a look into the manual for Instantiate