1
votes

I am trying to instantiate the following object to a child of the gameobject this script resides:

 var newEnergyPart = Instantiate<GameObject>(energyPart, energyBarLayout.transform.position, Quaternion.identity);
           newEnergyPart.transform.SetParent(energyBarLayout.gameObject.transform);

However, It keeps giving me this error:

Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'BarUnit(Clone)'). UnityEngine.Transform:SetParent(Transform)

I thought that making sure to set parent to an object set as n instantiated gameobject would make the error go aways it keeps appearing.

1
Include more of your code. Is energyBarLayout a prefab?Draco18s no longer trusts SE

1 Answers

3
votes
  • It looks like 'energyBarLayout' script attached to Prefab object, you cannot set 'Prefab' as parent of instantiated transform so should check and change 'energyBarLayout'.

  • In your case, you tried to create 'newEnergyPart' before instantiate parent object so I think you should instantiate 'energyBarLayout' GameObject first and then create 'newEnergyPart' so It would work definitely.