Traditionally microorganisms were considered to be autonomous organisms that could be studied in isolation. However, over the last decades cell-to-cell communication has been found to be ubiquitous. By secreting molecular signals in the extracellular environment microorganisms can indirectly assess the cell density and respond in accordance. In one of the best-studied microorganisms, Bacillus subtilis, the differentiation processes into a number of distinct cell types have been shown to depend on cell-to-cell communication. One of these cell types is the spore. Spores are metabolically inactive cells that are highly resistant against environmental stress. The onset of sporulation is dependent on cell-to-cell communication, as well as on a number of other environmental cues. By using individual-based simulations we examine when cell-to-cell communication that is involved in the onset of sporulation can evolve. We show that it evolves when three basic premises are satisfied. First, the population of cells has to affect the nutrient conditions. Second, there should be a time-lag between the moment that a cell decides to sporulate and the moment that it turns into a mature spore. Third, there has to be environmental variation. Cell-to-cell communication is a strategy to cope with environmental variation, by allowing cells to predict future environmental conditions. As a consequence, cells can anticipate environmental stress by initiating sporulation. Furthermore, signal production could be considered a cooperative trait and therefore evolves when it is not too costly to produce signal and when there are recurrent colony bottlenecks, which facilitate assortment. Finally, we also show that cell-to-cell communication can drive ecological diversification. Different ecotypes can evolve and be maintained due to frequency-dependent selection.