触发... Spawn ...摧毁Unity 3D

时间:2014-10-27 20:59:54

标签: c# triggers unity3d destroy

我遇到了一个小问题。一击中我就想要一个敌人产卵。我只希望场上有一个敌人。现在,当我再次击中扳机时,我希望场上的敌人在下一个敌人即将产卵时自行摧毁它。关于如何做的任何想法?我是否使用" Destroy"对此分配?继承人我所拥有的:

 public GameObject Enemy;
 public float mytimer;


 void Start()
 {
     GameObject player = GameObject.Find("Player");
 }

 void spawnEnemy() {
     Transform enemy;
     GameObject enemySpawnPoint =      GameObject.Find("EnemySpawn");
     enemy =  Instantiate(Enemy,enemySpawnPoint.transform.position,enemySpawnPo int.transform.rotation) as Transform; 
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.name == "EnemyTrigger") {
         mytimer = Random.Range(0,10);
         Invoke("spawnEnemy", mytimer);
         Debug.Log("Spawn Normal");

         }
     }

     }

1 个答案:

答案 0 :(得分:0)

您可以将敌人存储到私有变量中,并在每次实例化新变量之前将其销毁。这样你就只有一个。

private GameObject enemy = null;
void spawnEnemy() {
    if(enemy != null){
        Destroy(enemy);
    }
    GameObject enemySpawnPoint =      GameObject.Find("EnemySpawn");
    enemy =  Instantiate(Enemy,enemySpawnPoint.transform.position,enemySpawnPo int.transform.rotation) as GameObject; 
}