My code now:
Animator ContainerAnimator = this.GetComponent<Animator>();
ContainerAnimator.runtimeAnimatorController = Resources.Load("Assets/Cube") as RuntimeAnimatorController;
ContainerAnimator.Play("ContainerMoveUp");
I've got some dynamically created objects which have animators. these animators still don't have a controller in there which I'm trying to add, and it doesn't appear to work. any tips or tricks to do this? google is running out of answers
Resources.Load("Assets/Cube")
is likely returning null.
You have to put the animation controller in a folder called "Resources" in the Assets folder. You must spell that correctly. Move your animation Contoller into this "Resources" folder.
After this, remove the "Assets/" from the Resources.Load
function. The path to pass to that should be a relative path in the Resources folder.
If the animation controller name is "Cube", you can then load it like this:
Resources.Load("Cube")
instead of:
Resources.Load("Assets/Cube")
You might be loading the asset from the wrong directory or misplaced the filename. Here, you just remove the Assets/
prefix from the code to resolve the issue.
You can try the following implementation as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayAnimationDemo : MonoBehaviour
{
[SerializeField]
Animator anim; // refer the animator via inspector
void Start() {
PlayAnimation(anim, "Cube", "ContainerMoveUp");
}
// animator is the animator object referred via inspector
// fileName is the animator controller file name which should put under the Resources folder
// animName is the animation clip name, you want to play
void PlayAnimation(Animator animator, string fileName, string animName) {
animator.runtimeAnimatorController = Resources.Load(fileName) as RuntimeAnimatorController;
animator.Play(animName);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With