- Edited
Hi guys,
I'm having a real headache when duplicating objects.
I have an object called "Item". At a certain point in my game I duplicate the node via Duplicate()
public virtual Item Clone(NPC npc)
{
var clonedItem = Duplicate() as Item;
npc.AddChild(clonedItem);
npc.RemoveChild(clonedItem);
return clonedItem;
}
I quickly add the new item to the scene tree and then remove it again so that the _Ready() method is actually executed.
That seems to work. However, every item has a child that displays UI elements.
The problem: After cloning, the background texture and text label of the original item are ALSO displayed where the new item is.
The label of the new item is not displayed at all.
Both in the editor tree and via the debugger in the code, it seems as if the UI elements are also separate objects.
Could it have something to do with the fact that the label and children are displayed in a subviewport? But according to the inspector, these are also independent objects.
_By the way: I have the strong feeling that AddChild followed by RemoveChild is a very bad style for clean cloning... But I don't know how else to get a clean execution of Ready().
Can anyone help me here? I'm totally lost.