How to set animator's controller in script?

How to set animator’s controller in script?

Animator animator = playerTransform.gameObject.GetComponent();
animator.runtimeAnimatorController = Resources.Load(“path_to_your_controller”) as RuntimeAnimatorController;

public RuntimeAnimatorController anim2;

this.GetComponent().runtimeAnimatorController = anim2 as RuntimeAnimatorController;

Hope this helps

source:

https://forum.unity3d.com/threads/swapping-runtimeanimatorcontroller-during-runtime.368048/

Way I found:

public Animator animator;
public RuntimeAnimatorController newController;

public void SetAnimator()
{
     animator.runtimeAnimatorController = newController;
}

Next time, check the scripting reference Here

Alas, I have come seeking the answer to this question, but the comments are all whited out. Is something wrong with this page?

Id like to know

Thankss a lot!!!

i did this, setting in the inspector the reference to my desired animator as animatorBoy and the character gameobject.
Hope it helps

public GameObject MyCharacter;
public RuntimeAnimatorController animatorBoy;

Animator animator = MyCharacter.GetComponent<Animator>();

animator.runtimeAnimatorController = animatorBoy;

thanks you