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];
}
}
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
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.