How do you add an existing Animator (Controller) to a New (Instantiated) GameObject? (C#)

So I have 2 animator controllers. 1 for the player, and 1 for the CPU. And when creating (instantiating) a new CPU or Player, I would like to assign the appropriate animator controller, since Unity seems to destroy the animator controller from my GameObjects that are “Don’tDestroyOnLoaded”.

I can’t seem to figure this out. The only thing I could find was the following post, but all the answers are blank! https://answers.unity.com/questions/400648/how-to-set-animators-controller-in-script.html

Thanks in advance.
iBenji

I was able to figure it out. :slight_smile:

Here’s what I did, which works great! (Not sure if it’s the “recommended” approach:

public static RuntimeAnimatorController p1Ann;

void Start () 
{

c1 = GameObject.Find("{your model}");  //this is your model/avatar/prefab, etc.  Just make sure that it has an initial controller set on the Animator Component.  (The one you wish to reuse/reference)

p1Ann = c1.GetComponent<Animator>().runtimeAnimatorController;

 DontDestroyOnLoad(c1);

}

//then, to set it in a different function.  (after instantiating)

GameObject p1 = Instantiate(c1);

var anC = p1.GetComponent<Animator>();
anC.runtimeAnimatorController = cpuAnn; 

//please note: my code is a little different, but this is the general idea, and it works!  :-)