I originally posted this question on answers.unity3d but got no answers Unity3d Instantiate a child prefab from the parent source
I have a Prefab called GreyPiece, When clicked on it, This GreyPiece is supposed to create children of the same type currently my GreyPiece class has a public Transform object called GreyPieceTransform
public Transform greyPieceTransform;
This Transform is the same as the main GreyPiece Prefab [dragged and dropped in the Unity3d Editor] And when clicking on the object, I Instantiate multiple children for this object [as needed] and setting the transform as it's parent
Transform greyPiece = Instantiate(greyPieceTransform, transform.position, transform.rotation) as Transform;
greyPiece.parent = transform;
Debug.Log("this id "+transform.GetInstanceID()+"\tprefab id "+greyPieceTransform.GetInstanceID()+"\tchild id "+greyPiece.GetInstanceID());
so far so good, if I create one object and click on it , I'd have 1 child
- parent
- child
now if I create 2 I'll get this
- parent
- child
- child
- child
if I create 3
- parent
- child
- child
- child
- child
- child
- child
- child
basically what's happening is that the original GreyPieceTransform seems to be changing, and when I try to instantiate another object it's taking the modified [the current parent] and instantiates from it this issue doesn't happen if I didn't set the parent of the new instantiated object
EDIT: I also added a debug output The debug.Log output is this
this id -185148 prefab id -185148 child id -185236
this id -185148 prefab id -185148 child id -185318
this id -185148 prefab id -185148 child id -185418
As you can see , the parent transform and the prefab transform have the same ids, but they shouldn't
To make this even clearer I decided to name the transforms as "grey "+greyPiece.getInstanceID(); and this is how it looks in the hierarchy view
so now, how can I have the grePieceTransform actually reference the prefab [instantiate from the prefab] instead of it having the same reference as the parent