- Home /
Instantiated GameObject gets spawned as a child
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);
}
}
Answer by PizzaPie · Jan 28, 2018 at 02:33 PM
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);
Answer by TukangSendal · Nov 05, 2019 at 07:17 AM
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;
Your answer
Follow this Question
Related Questions
Instantiate object as child of exsisitng game object 1 Answer
Accessing variable from parent 1 Answer
Renaming Child Objects during Instantiate? 1 Answer
"Center On Children" programmatically 1 Answer
Destroying childs and Instantiate [C#] 0 Answers