I am creating a code that makes an enemy drop items, but the problem is that I dont want to instantiate the item as a child of the enemy
For example:
Enemy1 ← Parent (Then, when its killed generates a child object, which is the item)
Item ← Child of enemy1
And since the enemy1 was killed, it will be destroyed, but the problem is that it also destroys the Item that it drops, since its a child from enemy.
The code I am using for this is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControl : MonoBehaviour , ILivingThing{
public Enemy thisEnemy;
void Update () {
//Some artificial intelligence here...
Damage(1);
}
public void Damage(float damageAmt){ //Implemented from ILivingThing
thisEnemy.Damage (damageAmt);
if (thisEnemy.Health <= 0) {
dropItem ();
Kill ();
}
}
public void Heal(float healAmt){ //Implemented from ILivingThing
thisEnemy.Heal (healAmt);
}
public void Kill(){ //Implemented from ILivingThing
thisEnemy.Kill ();
//Destroy (gameObject);
}
void dropItem(){
GameObject Item = Instantiate (Resources.Load ("Prefabs/Items/Item1"), gameObject.transform) as GameObject;
Item.GetComponent<ItemControl> ().thisItem = new Item1();
}
}
How can I Instantiate a GameObject on “top of hierarchy” to get rid of this problem?