简体   繁体   English

出于某种原因,我的代码不应该在我的僵尸中产生,我不知道为什么

[英]For some reason my code is spawning in my zombies when it is not supposed to and I don't know why

This script is supposed to spawn in my zombies in waves.这个脚本应该在我的僵尸中以波浪的形式产生。 My problem is that it spawns in the second wave when it isn't supposed to and I don't know why this is doing this.我的问题是它不应该在第二波中产生,我不知道为什么会这样做。 I know that enemyCount isn't 0 because when the game ran I checked and confirmed that it is over 0. Is it possible for someone to have a look at the code and tell me whats wrong.我知道enemyCount 不是0,因为当游戏运行时我检查并确认它超过0。是否有人可以查看代码并告诉我有什么问题。 I haven't been able to find anything.我什么都没找到。 Thanks!谢谢!

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;
        }
    }
    
}

I fixed it by preseting enemyCount to 1. I can assume that the problem was that the update somehow happened before the enemyCount updated(lag)我通过将敌人计数预设为 1 来修复它。我可以假设问题是更新发生在敌人计数更新(滞后)之前

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出于某种原因,我的僵尸尝试将 go 设置为 0 0 0 而不是玩家,我不知道为什么 - For some reason my zombies tries to go to 0 0 0 instead of the player and I don't know why 出于某种原因,我的代码将 2 添加到变量而不是 1 我不知道为什么 - For some reason my code adds 2 to a variable instead of 1 and I don't know why 我的 WindowsForm 只是忽略了一些代码行,我不知道为什么? - My WindowsForm just ignore some code lines and I don't know why? 出于某种原因,当我通过代码要求它时,我的 animation 没有播放 - For some reason my animation isn't playing when I want it to when I ask it to through code 在我的C#嗅探器中设置套接字选项时,不知道为什么我收到错误代码10022(无效参数) - Don't know why I'm getting Error Code 10022 (Invalid Argument) when setting socket options in my C# sniffer 为什么当我在 DataGrid 中做了一些更改时,我的 ObservableCollection 没有任何更改? - Why I don't have any changes in my ObservableCollection when I have made some in my DataGrid? 我不知道为什么我的班级无法序列化 - I don't know why my class cannot be serialized 我的猜谜游戏不起作用,我不知道为什么 - my guessing game is not working and i don´t know why Euler 549项目-我的函数没有返回应该返回的答案,而且我不知道出了什么问题 - Project Euler 549 - My functions are not returning the answer it's supposed to return and I don't know what's wrong 当运行基本的c#控制台应用程序时,控制台打开空白,我不知道为什么 - When running my basic c# console app, my console opens blank and I don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM