The sentence you wrote is almost perfect! Here's the sentence:
A hedgehog curls into a prickly ball when in danger.
This sentence is grammatically correct. It means that when a hedgehog feels scared or threatened, it curls up so its spiky body protects it.
Great job! If you want to make it even clearer for a 5-year-old, you could say:
When a hedgehog is scared, it curls up into a prickly ball.