Why is Instantiate not creating a new object?

private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag ==“Box”)
{
Instantiate(TradedObject, collision.gameObject.transform.position, collision.gameObject.transform.rotation, collision.gameObject.GetComponentInParent());
Destroy(collision.gameObject);
}
}


it is destroying to object when it collides but it’s not creating a new one. I can provide more info if needed ( which i’m sure it is)

Just gonna put this here to make it more clear

can you confirm your collision is being detected?

You are possibly setting the parent of the new object to an object you then destroy, including your own new object. Make sure the parent is what you think it is… GetComponentInParent can return the child itself:
Unity Docs

ZZodo Games


Solved the issue by changing GetComponentInParent() to collision.transform.parent = null which seperates the object from it’s parent before deleting the other one. Hope this is useful to anyone else who’s also having this issue!