Imagine a caterpillar, green and yellow and about 0.8 inches (2cm) long. A rich coat of fine white hair sprouts from its back and sides. And bizarrely, what looks like a horn sticks up from its head.

You now have a mental image of the Australian caterpillar known as the gum leaf skeletoniser (Uraba lugens). Their name refers to the caterpillars' destructive habit of stripping the leaves of Eucalyptus gum trees to mere veins and mid-ribs.

Behavioural ecologist Petah Low studied the caterpillars for her PhD at the University of Sydney, and eventually became rather fond of them. "I gave them a name," says Low. "Unicorn caterpillars." One writer has called them "Mad Hatterpillars".

The horn on a unicorn caterpillar is actually a stack of its old, empty head casings. Effectively, it is wearing a pile of its old skulls.

Low's research could now explain why it has adopted such unconventional headgear.

A caterpillar's body is a squishy sausage of flesh and fluid encased in a tough outer skin. As it grows it eventually gets too big for its own skin, at which point it molts: the caterpillar cracks the old skin, crawls out, and grows a new larger skin.

Each gum leaf skeletoniser molts up to thirteen times before spinning a cocoon and transforming into an adult moth. Starting from the fourth molt, the gum leaf skeletoniser keeps the head shells from its old skins and stacks them on its head.

It is not quite clear how the stack is held in place, but the caterpillar does have a crown of hairs that might help to anchor it.

That is a pretty unusual thing to do. Most insects discard their old skin, and some eat it, but only a handful of species make horns out of their old head shells. So why do they do it?

In a study published in 2014, Low probed gum leaf skeletonisers with forceps to simulate attacks. "They reared their heads back, as if they were trying to protect their backs with the head shell stack," says Low. "The stack extended the caterpillar's reach and knocked my forceps around."

So Low wondered if the head shell stack helps protect the caterpillar, an idea first proposed in the 1980s. To find out, she tested how the caterpillars use their horns. Her latest results have now been published in PeerJ.

Low placed two caterpillars in a petri dish, one with a head shell stack and one without, then introduced a predatory stinkbug into the arena and watched what happened.

The stinkbug probed the caterpillar with its rostrum, a needle-like mouth that injects toxins and sucks prey dry. In response the caterpillar thrashed about, swung its head, regurgitated food and retreated.

This video shows a typical attack.

This media cannot be played on your device.

The caterpillars without head shells succumbed within 14 seconds, but the caterpillars with head shells thwarted the predator for at least 120 seconds – although they also succumbed in the end.

Often, the bug stabbed the head shells instead of the caterpillar, suggesting that the horn confuses predators. Also, when the bug stabbed the soft flesh behind the armoured head, the caterpillar swung its horn to deflect the bug's needle.

It was a small study, so the results are inconclusive. But Low feels "very confident that the head shell stack played a part in prolonging the attack."

In the wild, the young caterpillars gather in groups. To find out if this made a difference, Low placed 600 caterpillars in groups of ten on the leaves of gum trees. One set of groups had all their horns removed, another had all its horns intact, and a third had an equal mix of both.

After eight days, only one in six caterpillars still lived. Groups of caterpillars with head shells did not survive better than those without. But within the mixed groups, twice as many caterpillars with head shells survived than those without head shells.

This suggests that a horn does not help a caterpillar much if most nearby caterpillars also have them. But in the wild, different caterpillars have different numbers of head shells. So a gum leaf skeletoniser's horn might just protect it, as long as its horn is better than those of its neighbours, says Low.