Magnesium and nitrogen combine to form magnesium nitride ##Mg_3N_2##.
Magnesium nitride is an ionic compound. Magnesium forms ions with a ##2+## charge and nitrogen forms nitride ions with a ##3-## charge. The overall charge of the compound must be zero. Therefore the numerical value of the positive and negative charge must be ##6##.
##3[Mg]^(2+) + 2 [N]^(3-)####rarr####Mg_3N_2##
##3xx(2+)=6+## and ##2xx(3-)=6-##
##(6+)+(6-)=0##
An easy way to determine the formula unit of an ionic compound is called the crisscross method as demonstrated in the video below.