From the Super Mario Wiki
Gnats are the main enemies in the minigame Gnat Attack. They are the only enemies in the game that are unable to cause any damage to Mario. They are the first enemies to appear in the game, and are the least threatening; just dashing across the screen when they come on. As they are unable to harm Mario's hand, Mario should concentrate on the more dangerous enemies before swatting these insects.
Gnats will randomly appear in the editing mode of Super Mario Maker. They buzz around the screen, and can be swatted with a fly swatter, and hitting them will trigger a recreation of the Gnat Attack minigame. Gnats can also be spawned by shaking a Muncher and quickly trigger Gnat Attack.