Magnetic fields can travel through solids, liquids, gases, and through a vacuum. So magnets can work under water and in space.