In any population in which resources are limiting, the allocation of resources toward increased reproductive success may generate costs to survival [1-8]. The relationship between a sexually selected trait and fitness will therefore represent a balance between its relative associations with fecundity versus viability [3, 6, 7]. Because the risk of mortality in a population is likely to be heavily determined by ecological conditions, survival costs may vary as a function of the prevailing environment [7]. As a result, for populations experiencing heterogeneous ecological conditions, there may not be a single optimal level of allocation toward reproduction versus survival [9]. Here, we show that early viability and fecundity selection act in opposing directions on a secondary sexual trait and that their relative magnitude depends upon ecological conditions, generating fluctuating selection. In a wild population of Soay sheep (Ovis aries), phenotypic and genetic associations between male horn growth and lifetime reproductive success were positive under good environmental conditions (because of increased breeding success) and negative under poor environmental conditions (because of reduced survival). In an unpredictable environment, high allocation to early horn growth is a gamble that will only pay off if ensuing conditions are favorable. Such fluctuating selection may play an important role in preventing the erosion of genetic variance in secondary sexual traits.