Why it happens
Fudge depends on controlled crystallisation. When it's made well, the sugar forms very tiny crystals that feel smooth and creamy. If some sugar wasn't dissolved at the start, or crystals on the side of the pan fall back in, or the fudge is stirred or beaten while it's still too hot, large crystals form and the texture turns grainy. Not cooking it to the right stage can also affect how it sets.