- Home /
Instantiate New Gun
I am trying to make a gun pick up script so. So far I have it so that if I press right mouse click it will instantiate my gun prefab as a child of where the gun spawned from. the only problem is that when I run it and right click I get an error that says
"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."
I don't know what to do someone please help me. Here is my code
var gun : Transform;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Instantiate(gun, transform.position, transform.rotation);
transform.parent = gun.transform;
}
}
Answer by OperationDogBird · Aug 29, 2012 at 03:20 AM
You are parenting the prefab rather than the new instance. Do this
if (Input.GetButtonDown("Fire1"))
{
var newGun=Instantiate(gun, transform.position, transform.rotation);
transform.parent = newGun.transform;
}
Your answer
Follow this Question
Related Questions
inserting random gun into player/enemies hands 3 Answers
Parenting GameObjects 1 Answer
Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer
Change Parent of Multiple Instantiated Objects 1 Answer
How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer