[英]GameObject can't be found once per frame, even though it exists
at the moment I am creating a wave based game but I have a problem when it comes to checking whether all the enemies were killed.目前我正在创建一个基于波浪的游戏,但是在检查是否所有敌人都被杀死时我遇到了问题。 I have three different Enemies with the Tags "Enemy1", "Enemy2" and "Enemy3".我有三个不同的敌人,标签为“Enemy1”、“Enemy2”和“Enemy3”。 Every second, I am using GameObject.FindGameObjectWithTag("Enemy1") == null to check if all enemies were killed.每秒钟,我都在使用GameObject.FindGameObjectWithTag("Enemy1") == null检查是否所有敌人都被杀死。
Below you can find my code for better understanding:您可以在下面找到我的代码以便更好地理解:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Level1WaveSpawner : MonoBehaviour
{
public enum SpawnState { SPAWNING, WAITING, COUNTING }
public Waves[] Waves;
private int nextWave = 0;
public Transform SpawnPointE1;
public Transform SpawnPointE2;
public Transform SpawnPointE3;
public float timeBetweenWaves = 2f;
private float waveCountdown;
private float searchCountdown = 1f;
private SpawnState state = SpawnState.COUNTING;
void Start()
{
waveCountdown = timeBetweenWaves;
}
void Update()
{
Debug.Log(EnemyIsAlive());
if (state == SpawnState.WAITING)
{
if (!EnemyIsAlive())
{
WaveCompleted();
}
else
{
return;
}
}
if(waveCountdown <= 0)
{
if(state != SpawnState.SPAWNING)
{
StartCoroutine(SpawnWave(Waves[nextWave]));
}
}
else
{
waveCountdown -= Time.deltaTime;
}
}
void WaveCompleted()
{
Debug.Log("Wave Completed");
state = SpawnState.COUNTING;
waveCountdown = timeBetweenWaves;
if (nextWave + 1 > Waves.Length - 1)
{
nextWave = 0;
Debug.Log("Completed all Waves");
}
else
{
nextWave++;
}
}
bool EnemyIsAlive()
{
searchCountdown -= Time.deltaTime;
if(searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("Enemy1") == null && GameObject.FindGameObjectWithTag("Enemy2") == null && GameObject.FindGameObjectWithTag("Enemy3") == null) ;
{
Debug.Log("All Enemies Killed");
return false;
}
}
return true;
}
IEnumerator SpawnWave(Waves _wave)
{
Debug.Log("Spawning Wave: " + _wave.name);
state = SpawnState.SPAWNING;
for(int i = 0; i < _wave.count; i++)
{
SpawnEnemy(_wave.enemy);
yield return new WaitForSeconds(1f / _wave.rate);
}
state = SpawnState.WAITING;
yield break;
}
void SpawnEnemy(Transform _enemy)
{
Transform _sp = null;
Debug.Log("Spawning Enemies: " + _enemy.name);
if(_enemy.transform.tag == "Enemy1")
{
_sp = SpawnPointE1;
}
else if (_enemy.transform.tag == "Enemy2")
{
_sp = SpawnPointE2;
}
else if (_enemy.transform.tag == "Enemy3")
{
_sp = SpawnPointE3;
}
Instantiate(_enemy, _sp.position, _sp.rotation);
}
}
The problem that I have is that every single second, the Method returns false (only for one frame) and then switches back to true, even though the Enemies exist the whole time.我遇到的问题是,每一秒,该方法都会返回 false(仅针对一帧),然后切换回 true,即使敌人一直存在。 There is no other code blocking or changing the state of the enemies, and the enemies are tagged.没有其他代码阻止或更改敌人的state,并且标记了敌人。 Do you have any idea why this is happening?你知道为什么会这样吗?
Another strange thing I found while experimenting is the following:我在实验时发现的另一个奇怪的事情是:
Debug.Log("Enemy1 Exists: " + GameObject.FindGameObjectWithTag("Enemy1");
if (GameObject.FindGameObjectWithTag("Enemy1") == null) ;
{
Debug.Log("Entered If Loop");
Debug.Log("Enemy1 Exists: " + GameObject.FindGameObjectWithTag("Enemy1");
}
The results were the following:
Enemy1 Exists: Enemy1 (Clone)
Entered If Loop
Enemy1 Exists: Enemy1 (Clone)
It seems like the logic on your code is correct, the only problem I could find in the code you provided was here看来您的代码逻辑是正确的,我在您提供的代码中能找到的唯一问题是这里
if (GameObject.FindGameObjectWithTag("Enemy1") == null && GameObject.FindGameObjectWithTag("Enemy2") == null && GameObject.FindGameObjectWithTag("Enemy3") == null) ;
More specifically in the last character, where there is a semicolon, which means that in the case that the if condition is true, nothing will be done, since the code "attached" to it is an empty line followed by a semicolon.更具体地说,在最后一个字符中,有一个分号,这意味着在 if 条件为真的情况下,什么都不做,因为“附加”到它的代码是一个空行,后跟一个分号。 The curly brackets that you put after the if aren't actually connected to the if clause in this case, which means that the code inside them will be executed everytime, independently of the enemies being dead or not.在这种情况下,您放在 if 之后的花括号实际上并没有连接到 if 子句,这意味着每次都会执行其中的代码,而与敌人是否死亡无关。 If you simply remove this semicolon at the end everything should work.如果您只是在最后删除这个分号,一切都应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.