if statement running both lines

I need help with this It is an if statement that is supposed to make it so the solar system skips the lunar eclipse based on a random chance but for some reason it runs both codes and the moon never uses the lunar eclipse phase in the sky.

            if (moonSprite.sprite != moonSprites[moonPhase - 1])
            {
                if (moonPhase == 32)
                {
                    int x = Random.Range(0, 4);

                    if (x != 4)
                    {
                        Debug.Log("Solar Eclipse Skipped");
                        moonPhase = 1;
                    }
                    else
                        moonSprite.sprite = moonSprites[32]; Debug.Log("Solar Eclipse");
                }
                else
                    moonSprite.sprite = moonSprites[moonPhase - 1];
            }

When you skip curly braces it’s only treating the very first statement as belonging to the else. Below is the way C# sees the code you’ve written. You’ll need to add braces around line 14 in your code to have both statements assigned to the else.

if (moonSprite.sprite != moonSprites[moonPhase - 1])
{
    if (moonPhase == 32)
    {
        int x = Random.Range(0, 4);

        if (x != 4)
        {
            Debug.Log("Solar Eclipse Skipped");
            moonPhase = 1;
        }
        else
        {
            moonSprite.sprite = moonSprites[32];
        }
    
        Debug.Log("Solar Eclipse");
    }
    else
    {
        moonSprite.sprite = moonSprites[moonPhase - 1];
    }
}

Are you referring to the 2 Debug Log statements being printed?
Exactly what i was thinking, re: previous poster :slight_smile:

oh duh, i should have seen that, i will re test this and see what happens

Edit: it still skips the lunar eclipse every time tested a full cycle about 7 times it should have statistically been through the eclipse at least once but it does not run both debug lines anymore

:slight_smile:

I set it to a random range of 0 to 0 and it still skipped it, does anybody see anything wrong with my code?

I’m sorry, could you post the updated code with 0,0 and the skip?

sorry its a 0 to 1 range but i tested it 5 times still, so it should be working,

            if (moonSprite.sprite != moonSprites[moonPhase - 1])
            {
                if (moonPhase == 32)
                {
                    int x = Random.Range(0, 1);

                    if (x != 1)
                    {
                        Debug.Log("Solar Eclipse Skipped");
                        moonPhase = 1;
                    }
                    else
                    {
                        moonSprite.sprite = moonSprites[32];
                        Debug.Log("Solar Eclipse");
                    }
                }
                else
                {
                    moonSprite.sprite = moonSprites[moonPhase - 1];
                }
            }

Integer random range is exclusive… 0,1 just equals 0.
0,2 would be 0 and/or 1 :slight_smile:

haha, i just realized that that was in your original post, too :slight_smile: Ooops. Because I got carried away with the Debug log I lost sight of that :slight_smile:

yeah but read the code even if 0 to 1 does always equal 0 it should always be a lunar eclipse every time, it does not matter what the range is (I would like it to be 25% when i get it working properly) it is never a lunar eclipse. I just cannot see why for see reason.

edit: never mind i figured it out using a random range of 1 to 4 works for this, usually i use a random range of 0 to the length of an array i have never really used it like this before. thanks for teh help everyone.

Glad ya got it working :slight_smile: