Reputation: 4459
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
Upvotes: 2
Views: 5115
Reputation: 39194
I guess the problem is the following. At the first instantiation transform
and greyPieceTransform
point to the same object.
You can check it comparing instance ids.
If this is the case the behavior you see is reasonable. The first time you instantiate a new GameObject
the source object has no parent and no children so you get just one child. The second time you are instantiating the parent with 1 child and attaching it as a child (fig.2).
And so on.
Here's a simple snippet that may help:
public class Test : MonoBehaviour {
public Transform prefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("prefab id " + prefab.GetInstanceID() + " this id " + transform.GetInstanceID());
}
}
}
It will print different combinations of ids depending on how the object references are set up.
Case 1
If Test
is attached to a object in the hierarchy
and prefab
is a reference to a prefab in the project, ids will be different.
Case 2
If Test
is attached to a object in the hierarchy
(which is a prefab instance)and prefab
is a reference to the linked prefab in the project id will still be different.
(prefab is a link from a instanced object to a prefab. it should be a prefab property override in this case and you should see the bold font in the inspector).Ids will be different.
Case 3
If Test
is attached to a prefab and the field prefab points to itself, when it is instantiated the first time(or dragged into the hierarchy), transform and prefab fields will point to the same object (the transform of the prefab instance).
Ids will be equal.
apparently what's happening is that the when instantiating an object from a that prefab Transform [greyPieceTransform], it's using it as the current object and modifies the original one [so greyPiece and greyPieceTransform would be the same]
No. If greyPieceTransform
and transform
of the object are different the "original" won't be modified. But probably they aren't.
Upvotes: 0