Is there a way to create an animation controller through script? I have an animation that I want playing on many different objects, pretty much every object I make in my game.
For convenience sakes, I wrote a script that adds an animation controller and plays a generic scale altering animation, kinda a popup animation whenever an object appears. I figured out how to add an animator, assign a new controller(through the inspector), and play the animation from that controller. It’s only really a two step process, but is there a way to make it so I can just plop this script on any object I want animating and have it work without assigning an avatar from the inspector?
Even if I can’t “create” a new controller, maybe there’s a way to assign the one that I’ve already created through script? I.E., access the animation controller through the resources folder or even have a “container” that just holds the controller and copy it over?
Thanks in advance.
Edit: Forgot to mention, I have tried putting an animator on an empty, with an animator and controller on it, assigning said controller to the current animator. But it always just returns null. Here’s my script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PopupAnimate : MonoBehaviour
{
Animator anim;
[SerializeField]
RuntimeAnimatorController controller;
void OnEnable()
{
if(controller = null)
{
controller = GameObject.Find("Animator Container").GetComponent<Animator>().runtimeAnimatorController;
}
anim = GetComponent<Animator>();
if(anim == null)
{
anim = gameObject.AddComponent<Animator>();
}
anim.runtimeAnimatorController = controller;
anim.Play("Popup");
}
}
Edit(again): sorry, for some reason checking if the controller was null was causing it to not be called? So I just have it not check if it’s null, now it works just fine!