How can I Instantiate a parent object in a child object?

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?

Hi,
here the doc Unity - Scripting API: Object.Instantiate.

It says :

public static Object Instantiate(Object original, Transform parent);

So if you want to instantiate your GameObjet on top of hierarchy (ie : a GameObject with no Parent), your dropItem() could be :

GameObject Item = Instantiate (Resources.Load ("Prefabs/Items/Item1"), null) as GameObject;

In a general way :

transform.parent = null; // will move the transform to the root of your scene.

Hope it will help you,

bye :slight_smile: