Unions don't necessarily do that.
Many have no worker participation to speak of that would breed class consciousness. If anything those unions make workers anti-union. They go on strike when the leadership says so, but usually they don't because leadership is friendly with management.
Even in unions with good participation, there's a limit. They learn the class consciousness against their own upper management but not against the class. Or they think of themselves as PMC-ascendant and not really in need of a union in the first place. I've had so many conversations with people who have a union and then look down on janitors.
Unions can be a vehicle for class consciousness but only through militancy, political education, and an org willing to push those things.