Animals often change their habitat throughout ontogeny; yet, the triggers for habitat transitions and how these correlate with developmental changes - e.g. physiological, morphological and behavioural - remain largely unknown. Here, we investigated how ontogenetic changes in body coloration and of the visual system relate to habitat transitions in a coral reef fish. Adult dusky dottybacks, Pseudochromis fuscus, are aggressive mimics that change colour to imitate various fishes in their surroundings; however, little is known about the early life stages of this fish. Using a developmental time series in combination with the examination of wild-caught specimens, we revealed that dottybacks change colour twice during development: (i) nearly translucent cryptic pelagic larvae change to a grey camouflage coloration when settling on coral reefs; and (ii) juveniles change to mimic yellow- or brown-coloured fishes when reaching a size capable of consuming juvenile fish prey. Moreover, microspectrophotometric (MSP) and quantitative real-time PCR (qRT-PCR) experiments show developmental changes of the dottyback visual system, including the use of a novel adult-specific visual gene (RH2 opsin). This gene is likely to be co-expressed with other visual pigments to form broad spectral sensitivities that cover the medium-wavelength part of the visible spectrum. Surprisingly, the visual modifications precede changes in habitat and colour, possibly because dottybacks need to first acquire the appropriate visual performance before transitioning into novel life stages.