I can't figure out why my else if statement is being ignored?

Okay so as I have posted before I am making a pokemon game based on a tutorial as a learning experience. I managed to create a day and night system based on the time of the computer running the game, and ascribed a variable that determines if it insight or day.
I also have a script applied to the map that creates a list of wild pokemon that can be encountered that is be filled out in the inspector. Now the problem is I tried to use the day/night variable to make two different lists of wild pokemon you can encounter based on the time. I tired to call the is it night or day variable from another script but I am not sure it is working.
Here is the script.

{
    [SerializeField] List<Pokemon> wildPokemonsDay;
    [SerializeField] List<Pokemon> wildPokemonsNight;

    public Pokemon GetRandomWildPokemon()
    {
        if (FindObjectOfType<TimeKeeper>().isNight)
        {
            var wildPokemon = wildPokemonsDay[Random.Range(0, wildPokemonsDay.Count)];
            wildPokemon.Init();
            return wildPokemon;
        }
        else
        {
            var wildPokemon = wildPokemonsNight[Random.Range(0, wildPokemonsNight.Count)];
            wildPokemon.Init();
            return wildPokemon;
        }

    }
}

So if I run the game with this only if (FindObjectOfType().isNight) is run. The else does not; I know this because I ran a debug.Log, and it never got triggered even when the game was day. I have two possibilities as to the cause. First, the (FindObjectOfType() does not work as intended, or The way the GetRandomWildPokemon() is written, it can not have two return results.
Is either of these conclusions right, or is it something else entirely?
Also, It won’t let me make the else statement an else if in the function.

Then debug it! At the very minimum put Debug.Log(“I AM HERE!”) statements in each return codepath.

2 Likes

Or better yet, attach the debugger, put a breakpoint and check the values in your condition:

2 Likes

I’m honestly not sure what the question is here. You’re kind of rambling.

Also I think your logic is backwards. You’re checking if it’s night, but you’re pulling from the day time wild pokemon? Is that just the issue there?

In any case you could probably consolidate the code to be less repeated:

public Pokemon GetRandomWildPokemon()
{
    var timeKeeper = FindObjectOfType<TimeKeeper>(); // probably could be a singleton pattern
    bool isNight = timeKeeper.isNight;
   
    var pokemonList = isNight ? wildPokemonsNight : wildPokemonsDay;
   
    int count = pokemonList.Count;
    int randomIndex = Random.Range(0, count);
    var pokemon = pokemonList[randomIndex];
   
    pokemon.Init(); // shouldn't we be copying the pokemon???
    return pokemon;
}
2 Likes

The most primitive data type is a boolean value. There’s no arguing about it’s state. If your else is never hit, that boolean value is most likely never false. So the best way to get the “first” step of your debugging journey is to do:

    public Pokemon GetRandomWildPokemon()
    {
        bool isNight = FindObjectOfType<TimeKeeper>().isNight;
        Debug.Log("GetRandomWildPokemon:: isNight is " + isNight);
        if (isNight)
        // [ ... ]

Now whenever this method is called, you will see the state of “isNight”. If this is never false, you have to go back to where / whenever this gets set (or if it’s a property, check what it’s doing).

1 Like