c# - 并非所有代码路径都返回值: Unity

我有一段正在编译的错误代码

"Not all code paths Return a Value"



我不知道该如何解决。有任何想法吗?这是我的代码。
bool EnemyIsAlive()
{
    searchCountdown -= Time.deltaTime;
    if (searchCountdown <= 0f)
    {
        searchCountdown = 1f;
        if (GameObject.FindGameObjectWithTag("Enemy") == null)
        {
            return false;
        }
    return true;
    }
}

最佳答案

EnemyIsAlive()应该为所有可能的条件返回一个 bool(boolean) 值;就你而言如果if (searchCountdown <= 0f)评估为false,则该方法将不返回任何内容。因此,您需要为错误条件添加return语句。根据您要处理的情况,它可能为true/false,但是应该有一个返回值。

bool EnemyIsAlive()
{
    searchCountdown -= Time.deltaTime;
    if (searchCountdown <= 0f)
    {
        searchCountdown = 1f;
        if (GameObject.FindGameObjectWithTag("Enemy") == null)
        {
            return false;
        }
    return true;
    }
  return false; // one line added to solve the error
}

https://stackoverflow.com/questions/36257249/

相关文章:

java - 变量名称不能在Eclipse中解析为变量错误(多重继承)

c++ - 代码块错误

c++ - 尝试编译我的wxWidgets程序时出现 “is_enum not declared i

c# - 错误 'Cannot Convert double to int'

java - 编译错误: Can not convert boolean into an int

c++ - 具有来自不同 header 的模板类的类将无法编译

java - BufferedReader错误

c# - 编译错误-您是否缺少using指令或程序集引用?

c++ - 奇怪的错误: undefined reference to `class::class(

c++ - C++构建错误: class does not name a type