Why the Animator Controller is vanish from the Inspector and not assigned when running the game?

Daniel Lip

It is assigned in the editor but when running the game in run time the Animator controller is gone and not assigned anymore.

I'm trying to play all the state animations in Animator controller one by one.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEditor.Animations;

public class SwitchAnimations : MonoBehaviour
{
    public AnimatorController controller;

    private Animator animator;
    private AnimatorState[] states;

    // Use this for initialization
    void Start()
    {
        animator = GetComponent<Animator>();
        states = GetStateNames(animator);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            StartCoroutine(QueueAnim(states));
        }
    }

    private AnimatorState[] GetStateNames(Animator animator)
    {
        controller = animator ? animator.runtimeAnimatorController as AnimatorController : null;
        return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
    }

    IEnumerator QueueAnim(params AnimatorState[] anim)
    {
        int index = 0;

        while (index < anim.Length)
        {
            if (index == anim.Length)
                index = 0;

            animator.Play(anim[index].name);

            AnimatorStateInfo si = animator.GetCurrentAnimatorStateInfo(index);
            yield return new WaitForSeconds(5);
            index++;
        }
    }
}

First all the variable in the script was private and this one was static :

private static AnimatorController controller;

And the method GetStateNames was static too :

private static AnimatorState[] GetStateNames(Animator animator)

But I changed the controller variable to be public and not static so the GetStateNames is not static now since the script is attached to empty GameObject.

But when I assign Animator Controller to the controller it's vanish when running the game.

Screenshot before running the game :

Before running the game

After running the game

jiveturkey

It appears you are assigning an AnimatorController and not an Animator. An Animator references/uses an AnimatorController but not the other way around. Just make Animator public and assign it the AnimatorController.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do carets vanish when calling a batch file from powershell?

Why when creating new animator controller for the character the character is not walking right?

Why when adding a Character Controller to npc the npc is in the air when running the game?

Why the character in 2d game falling when running the game ?

why the bar does not vanish when height is set to 0%

How to call a function when reference is assigned in custom inspector?

Move label from one position to another in unity when the game is running

How to check if a certain animation state from an animator is running?

Change Animator Controller by Script

Animator is not playing an animation controller

Why some properties are hidden from Object Inspector when more than one item is selected?

Why is Chrome coloring my cookie red when I set it from the inspector?

Can't Assign Game Object from inspector on Unity

How to clean up events assigned from controller?

model assigned by controller not reachable from component

How to avoid killing the running application when starting the Appium Inspector session?

Why does my Markercluster vanish on a stereographic projection?

Why is my Windows application/game capped at 30FPS when running on an Intel GPU?

Why is the 'game' module running automatically when I am trying to run 'FootballSim_server' module?

Why does an array behave differently when directly assigned or retrieved from get-content

nswindow animator not working in window controller

Why there is no error when a final int is assigned to a byte

Why will this cookie not change it's value when assigned?

Why is a function called when it is assigned to a variable?

Why data is not updated from a controller to another when set with service in AngularJS?

Why is a Future never completed when running from IntelliJ

Why is the stdout empty when running "python --version" from groovy?

Why does my script fail when running from udev?

Why does the nanmean function error when running an example from the documentation?