Change Animator Controller by Script

user3182651 picture user3182651 · Dec 6, 2014 · Viewed 15.6k times · Source

I have 2 Controllers may

1-ControllerBLUE.controller (Default)

2-ControllerRED.controller

How can I change the controller from script

I tried:

var colorController = GetComponent(Animator);


 colorController.runtimeAnimatorController =   Resources.Load("main/colors/controllercolors/ControllerRED.controller ") as RuntimeAnimatorController;

But it doesn't work it just make the animator controller to : "None (Runtime Animator Controller)"

Is it possible ? How can I make it work ?

Answer

Giulio Pierucci picture Giulio Pierucci · Jan 18, 2015

I'm sorry, I use C# in Unity:

using UnityEngine;
using System.Collections;
public class ChangeController : MonoBehaviour {

Animator animator;

// Use this for initialization
void Start () {
    animator = gameObject.GetComponent<Animator>();
    animator.runtimeAnimatorController = Resources.Load("main/colors/controllercolors/ControllerRED") as RuntimeAnimatorController;
}

// Update is called once per frame
void Update () {

} }