我有一个敌人和一个简单的脚本(如果是hp <= 0
,则敌人被摧毁(。我希望敌人在死后生成一个预制的,这可以让你在库存中拿起一把枪,但如果你通过创建它
Instantiate(pistolprefab, GameObject.transform);
则该枪被创建为"枪";"孩子";也被敌人摧毁。
public GameObject snipprefab;
public GameObject pistolprefab;
public GameObject rocketlauncherprefab;
public int TypeOfGun = 0;
private bool gunSpawned = false;
public static float HP = 10;
public void Start()
{
}
public void AddDamage(float damage)
{
HP -= damage;
if (HP <= 0)
{
if (EnemyHealth.HP <= 0 && gunSpawned == false)
{
switch (TypeOfGun)
{
case 1:
Instantiate(pistolprefab, gameObject.transform);
break;
case 2:
Instantiate(snipprefab, gameObject.transform);
break;
case 3:
Instantiate(rocketlauncherprefab, gameObject.transform);
break;
}
gunSpawned = true;
}
Destroy(gameObject);
}
}
只需在新游戏对象的转换组件上使用SetParent()
将对象移动到不同的父对象即可
GameObject temp = Instantiate(pistolprefab, gameObject.transform);
temp.transform.SetParent(null); // set parent to null if you don't want it to have a parent
在销毁原始对象之前,请确保执行此操作。