RuntimeAnimatorController not loading from script
Hi, I have a problem when loading a RuntimeAnimatorController from the script. I have been looking throug any avialable forum or tutorial on internet but I still haven't found the solution.
public RuntimeAnimatorController animSit;
public void animar(GameObject paciente)
{
Animator sentado = paciente.GetComponent<Animator>();
//sentado.runtimeAnimatorController = Resources.Load("Assets/animControl/H_B_obeso_contr") as RuntimeAnimatorController;
sentado.runtimeAnimatorController = animSit;
}
Both of the ways I'm trying to add the AnimatorController doesn't work. It keeps the AnimatorController in blank.
If during runtime I add the Controller through the Inspector, it works!! but I need to add it from the script because I'm instantiating the GameObject.
I really apreciate if you have any kind of solution, been trying to solve this for a long time.
Answer by TXrTim · Jul 04, 2018 at 06:06 AM
I don't agree with "Best Answer".
Currently I'm on latest beta 2018.2.0b9 Personal and here's what could be the root cause: If my RuntimeAnimatorController is not placed in \Resources\ folder script will behave just like in your situation - no errors, no warning and if you select animator in inspector it still "None". BUT! After i placed RuntimeAnimatorController in \Resources\ everything started working.
Summary:
Step 1: Place RuntimeAnimatorController into \Resources\ folder. Lets say file name will be "MyController.controller".
Step 2:
var animator = gameObject.AddComponent<Animator>();
animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("MyController");
Nice! Simple and usefull solution.
BTW, it's not an error or something like a bug. Official Wiki: "Load - Loads an asset stored at path in a Resources folder." https://docs.unity3d.com/ScriptReference/Resources.Load.html
Answer by SepraB · Oct 15, 2016 at 02:11 AM
After many tries, the answer is simple.
By this version of Unity, there is an obligation where you have always to set the controller before you run the scene. It is impossible to add the controller to the RunTimeAnimatorController without a a controller.
Just answering myself because if someone have the same answer.
Answer by ds5 · Oct 19, 2017 at 08:16 PM
Hi, I´m with the same problem now. So how could I set the controller before run the scene.
It has been a long time since that issue and I guess the problem still does not have any solution.
Answer by harko12 · Jan 30, 2019 at 06:57 AM
Hi there. I'm not sure if anybody is still having trouble with this. I think I have accomplished what what it was you were trying to do. Basically, I set up an Animator on my character, and then a set of SerializedObject's to hold the different RuntimeAnimiatorControllers I wanted to switch to. Then, at the appropriate time, I ran this basic code:
RuntimeAnimatorcontroller motionAnimator; // set up previously
// store the previous controller
originalAnimator = myController.myAnim.runtimeAnimatorController;
// change the runtimeAnimatorController on the Animator to the new controller
myController.myAnim.runtimeAnimatorController = motionAnimator;
That all worked for me as far as changing controllers when I wanted it to. However, once I got it to work, the problem then became smoothly animating between controllers. it got very jumpy. I'm now in the process of refactoring all of that out, and auto generating my AnimatorController based on some chosen sub state machines. But the switching did work.