i dont understand this
do you want it so that if your unit is attacking from range make it use gun but if its going melee then attack with a sword?
hmm i would make your unit have a regular attack with gun, have a minimal and maximal range, so that if the unit is too close you cant attack with your gun, and make an autocast ability that will activate on units close up that will use the units sword.