我试图创建一个简单的产卵器,产卵僵尸后有一些延迟。我为僵尸的数量设置了限制,但这并不奏效,因为创建的是12个僵尸,而不是10个。我可以通过替换 lenght <= max
关于 lenght <= max-2
不过 我也不知道问题的根源在哪里?.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
// Start is called before the first frame update
GameObject zombie;
bool condition;
public int max;
int lenght;
void Start()
{
zombie = Resources.Load("Prefabs/Zombie") as GameObject;
max = 10;
condition = true;
}
// Update is called once per frame
void Update()
{
GameObject[] zombies = GameObject.FindGameObjectsWithTag("Zombie");
lenght = zombies.Length;
Debug.Log(lenght);
if (condition && lenght <= max)
{
StartCoroutine(Spawn());
}
}
private IEnumerator Spawn()
{
condition = false;
while(lenght <= max){
yield return new WaitForSeconds(1);
Instantiate(zombie);
}
condition = true;
}
}
问题的根源 "是你的IF语句,它们都有一个...。小于或等于 (=<)运算符,这意味着它将停止,一旦它达到一个数字超过10,产生一个额外的僵尸。少于 (<)确保它的停止,如果数字是10,因为10不小于10。