-2

I have a Section of code that is compiling with the error of

"Not all code paths Return a Value"

I have no idea how to fix this. Any Ideas? Here is my code.

bool EnemyIsAlive()
{
    searchCountdown -= Time.deltaTime;
    if (searchCountdown <= 0f)
    {
        searchCountdown = 1f;
        if (GameObject.FindGameObjectWithTag("Enemy") == null)
        {
            return false;
        }
    return true;
    }
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Damon Neal
  • 1
  • 1
  • 2
  • Possible duplicate of [c# returning error "not all code paths return a value"](http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value) – Serlite Mar 28 '16 at 14:20

1 Answers1

1

The EnemyIsAlive() should return a boolean value for all possible conditions; in your case; The method will not return anything if if (searchCountdown <= 0f) evaluates to false. So you need to add a return statement for the false condition. It may true/false according to the scenario you are dealing with, but a return should be there.

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
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88