【问题标题】:For some reason my code is spawning in my zombies when it is not supposed to and I don't know why出于某种原因,我的代码不应该在我的僵尸中产生,我不知道为什么
【发布时间】: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


【解决方案1】:

我通过将enemyCount 预设为1 来修复它。我可以假设问题是更新发生在enemyCount 更新之前(滞后)

【讨论】:

  • 您应该弄清楚“不知何故”是什么,因为这将帮助您避免将来出现此类问题。我怀疑这与Instantiate有关。
  • 确保在发布之前至少花一个小时解决您的问题。
  • 你应该看看你的代码产生的波,它们都是一样的。遵循 DRY 原则将有助于您在未来减少大量工作。提示:确定发生了什么变化并将其隔离。
  • 您是否有可能在检查器中将enemyCount 变量预先设置为其他值?您已将其声明为 public,所以这似乎是最可能的原因...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
  • 2011-11-05
  • 2021-10-07
  • 2020-09-19
  • 2014-01-17
  • 1970-01-01
  • 2021-03-26
相关资源
最近更新 更多