【发布时间】:2021-05-03 21:53:17
【问题描述】:
这个脚本应该在我的僵尸中以波浪的形式生成。我的问题是它不应该在第二波中产生,我不知道为什么会这样做。我知道enemyCount 不为0,因为当游戏运行时我检查并确认它超过0。是否有人可以查看代码并告诉我出了什么问题。我什么都没找到。谢谢!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waves : Zombie
{
public int enemyCount;
public GameObject behemoth;
public GameObject runner;
public GameObject zombie;
private int xPos;
private bool HasSecondWaveRan;
private bool HasThirdWaveRan;
public int enemyCount2 = 1;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < 10; i++)
{
xPos = Random.Range(31, -14);
Instantiate(zombie, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
enemyCount = enemyCount + 1;
}
for (int i = 0; i < 3; i++)
{
xPos = Random.Range(31, -14);
Instantiate(runner, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
enemyCount = enemyCount + 1;
}
}
// Update is called once per frame
void Update()
{
if (enemyCount == 0)
{
StartCoroutine(SecondWave());
HasSecondWaveRan = true;
}
if (enemyCount2 == 0)
{
StartCoroutine(ThirdWave());
HasThirdWaveRan = true;
}
}
IEnumerator SecondWave()
{
if (HasSecondWaveRan == false)
{
yield return new WaitForSeconds(5.0f);
for (int i = 0; i < 5; i++)
{
xPos = Random.Range(31, -14);
Instantiate(runner, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
xPos = Random.Range(31, -14);
Instantiate(zombie, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
enemyCount2 = 25;
enemyCount = 1;
HasSecondWaveRan = true;
}
}
IEnumerator ThirdWave()
{
if (HasThirdWaveRan == false)
{
yield return new WaitForSeconds(5.0f);
for (int i = 0; i < 5; i++)
{
xPos = Random.Range(31, -14);
Instantiate(runner, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
xPos = Random.Range(31, -14);
Instantiate(zombie, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
HasThirdWaveRan = true;
}
}
}
【问题讨论】:
-
您还没有向我们展示所有代码。我们无法确认
Update没有在Start之前被调用(或者至少在开始到达enemyCount = enemyCount + 1;之前 - 鉴于我可以看到的代码,这很可能是您的问题的原因。尝试声明public int enemyCount = 1;并在Start末尾添加enemyCount--;以查看问题是否消失。 -
@Enigmativity 通常情况下可能是这种情况,但 Unity 为其预定义的方法提供了 defined execution order。
标签: c# visual-studio unity3d