I am trying to make an enemy that spawns a coin when it gets killed. But the coin gets spawned as a child to the enemy and when the enemy gets deleted the coin gets deleted as well.
How do I fix this?
public GameObject coin;
public Transform monsterDeathLocation;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "FireBall")
{
Destroy(collision.gameObject);
Instantiate(coin, monsterDeathLocation);
Destroy(gameObject);
}
}
If you read the documentation you ll notice that the overload you are using is setting the object as child of the transform you provide.
You need the 4th or 5th overload (as seen on docs) and it would look something like this
Instantiate(coin, monsterDeathLocation.position, Quaternion.identity);
just instantiate object on empty object,dont on your transform monster, u can add empty object and instatiate your coin if your collision is actived,
GameObject go = Instantiate(coin, monsterDeathLocation.position, Quaternion.identity, emptyobject.transform) as GameObject;