我的实例化在1或2次产生后停止,但我希望它们通过游戏永远产卵......
因为我在这个脚本中有破坏代码片段,但我能做什么我需要它们在碰撞后死掉,或者像以前一样在屏幕上有数百个。像往常一样,死敌必须死亡并消失。那我该怎么办?破坏代码正在摧毁整个敌方玩家/预制件而不仅仅是我想要的死亡实例。
我希望敌人继续产卵,但当然被玩家杀死时,他们必须消失。
2d游戏我在敌人预制件和场景敌人身上有这个剧本
我希望屏幕上的敌人在碰撞时一个接一个地死去,但是毁灭会杀死我想的整个物体/预制物,它不会再实例化/产生
我在这里有一些关于敌人的脚本是实例化脚本
使用UnityEngine; 使用System.Collections;
public class spn2 : MonoBehaviour {
GameObject Enemy;
//public GameObject EasyEnemey;
public GameObject MediumEnemey;
public GameObject HardEnemey;
public Transform[] SpawnPoints;
public float TimeBetweenSpawns;
public int NumberOfEnemiesToSpawn;
public int NumberOfMediumEnemiesToSpawn;
public float EasyChance;
public float MediumChance;
public float HardChance;
private int waveNumber;
private float spawnTimer;
private int numberOfEnemies;
private int numberOfMediumEnemies;
// Use this for initialization
void Start()
{
//this below is the time to spawn so if 4 , every 4 seconds 1 will spawn etc
this.spawnTimer = 3.0f;
this.waveNumber = 0;
float totalChance = this.EasyChance + this.MediumChance + this.HardChance;
if(Mathf.Abs(totalChance-1.0f)>0.0001f) {
Debug.LogWarning("Warning: The chances should add up to 1.0 ("+totalChance+" currently)");
}
}
// Update is called once per frame
void Update()
{
this.spawnTimer -= Time.deltaTime;
if(this.spawnTimer<=0.0f)
{
Transform spawnPoint = this.SpawnPoints[Random.Range(0, this.SpawnPoints.Length)];
Vector2 spawnPos = spawnPoint.position;
Quaternion spawnRot = spawnPoint.rotation;
switch(this.waveNumber)
{
case 0:
//Instantiate(EasyEnemey, spawnPos,spawnRot);
Instantiate(Resources.Load(Enemy) as GameObject, spawnPos, spawnRot);
this.numberOfEnemies++;
if(this.numberOfEnemies>=this.NumberOfEnemiesToSpawn)
{
this.waveNumber++;
}
break;
case 1:
Instantiate(MediumEnemey, spawnPos, spawnRot);
this.numberOfMediumEnemies++;
if (this.numberOfMediumEnemies >= this.NumberOfMediumEnemiesToSpawn)
{
this.waveNumber++;
}
break;
case 2:
float randomFloat = Random.value;
if(randomFloat<this.EasyChance)
{
Instantiate(Enemy, spawnPos, spawnRot);
}
else if(randomFloat<this.EasyChance+this.MediumChance)
{
Instantiate(MediumEnemey, spawnPos, spawnRot);
}
else
{
Instantiate(HardEnemey, spawnPos, spawnRot);
}
break;
}
this.spawnTimer = this.TimeBetweenSpawns;
Destroy (gameObject, .7f);
}
}
}
答案 0 :(得分:3)
您正在将预制件实例化为实际对象,因此当您删除实例时,您将删除预制件。请尝试使用此代码进行初始化。这将创建预制件的副本并将其作为实例加载。
GameObject enemy = Instantiate(Resources.Load("Name of your prefab here") as GameObject, spawnPos, spawnRot);
对于你的其他问题,你的东西开始产卵,让我们来看看你的代码。我删除了一些不相关的东西,但我只是复制并粘贴你的东西。看看你的代码在做什么。
每次更新(由Unity连续调用,我们可以近似为每帧一次),我们称这种方法为Update。它从SpawnTimer中减去一些时间,所以假设你的生成计时器是4秒,而Time.DeltaTime是1秒(它不是,但它是一个例子)。
所以你的更新会运行4次,这很好,但现在会发生什么?突然,你的生成计时器是0,这个条件
if(this.spawnTimer<=0.0f)
评估为True,因此我们准备实例化敌人。大。我们通过switch语句,选择我们的敌人,创建它们,然后我们调用Destroy,它会立即删除我们的gameObject。
public class spn2 : MonoBehaviour {
//variables here that I removed
//removed start method
// Update is called once per frame
void Update()
{
this.spawnTimer -= Time.deltaTime;
if(this.spawnTimer<=0.0f)
{
//instantiation stuff;
{
case 0:
Instantiate(Resources.Load(Enemy) as GameObject, spawnPos, spawnRot);
break;
case 1:
Instantiate(MediumEnemey, spawnPos, spawnRot);
break;
case 2:
//instantiation logic
break;
}
this.spawnTimer = this.TimeBetweenSpawns;
Destroy (gameObject, .7f); <-- Destroy is here? This is your problem!
}
}
}