This happens in many cases before the child is six years old and can occur in an instant.
One of the more bizzare examples is that of the people in London who were very young when the London blitz was on in 1940 and observed the adults donning gas masks to protect against potential attacks. Ever since, they get sexual arousal from seeing someone in a gas mask.
This is the effect of "imprinting."