我每次调用NewSpawn()时都试图实例化一个僵尸预制件,这时reSpawn变量变为'true'(通过检查器)。然而,一旦我实例化一个僵尸,它就会形成一个无限循环的僵尸克隆。 [克隆克隆克隆]如何将每个方法调用限制为一个僵尸实例化? (我想制作10个僵尸,而不是10,000个。 谢谢!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zombie : MonoBehaviour {
public GameObject zombiePrefab;
public Transform zombieSpawnPoint;
private Transform[] spawnPositions;
public bool reSpawn = false;
private bool lastToggle = false;
// Use this for initialization
void Start () {
spawnPositions = zombieSpawnPoint.GetComponentsInChildren<Transform>();
}
private void NewSpawn() //spawn location of newZombie
{
Instantiate(zombiePrefab, transform.position, transform.rotation);
int i = Random.Range(1, spawnPositions.Length);
transform.position = spawnPositions[i].transform.position;
}
void Update () { //T-toggle
if (reSpawn != lastToggle)
{
NewSpawn();
reSpawn = false;
}
else
lastToggle = reSpawn;
}
}
答案 0 :(得分:3)
只需在按钮点击事件中拨打NewSpawn()
,而不是将reSpawn
设置为true。
除此之外,这没有任何意义:
else
{
lastToggle = reSpawn;
}
他们已经平等了。