How to switch Animator controllers in script?

So basically I am trying to switch animator controllers for my player because it has a melee attack and a range attack. The melee attack and range attack have different animations. But the code in my script doesn’t seem to work.

This is the code:

if (UseSword == true) {
			Sword.SetActive (true);
			animator.runtimeAnimatorController = Resources.Load ("Assets/SpaceMan") as RuntimeAnimatorController;
			Gun.SetActive (false);

var ranged: boolean;
var myControllers: RuntimeAnimatorController;
var animator: Animator;

function Update()
{
          if(!ranged)
         {
                  animator = myControllers[0];
         }
         if(ranged)
         {
                  animator = myControllers[1];
         }
}