Can't assign active gameobjects to public variables in StateMachineBehaviour script from the inspector screen

Hi, I need some help!!

I made an animation controller, added some states, and added StateMachineBehaviour in one of the states.
I wanted to change texture when it enters certain state so I wrote some code inside the StateMachineBehaviour.

public class Smile : StateMachineBehaviour {
	public Texture2D smileTexture;
	public GameObject[] Eyes;

	 // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
	override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
		//FIXME : Why can't I assign the eyes in the inspector?!!!!!!!
//		eyes[0].GetComponent<Material>().mainTexture = smileTexture;
//		eyes[1].GetComponent<Material>().mainTexture = smileTexture;

	}
}

It shows error(about Serialized… I don’t remember) first, so I searched the solution and one says it should fix when Unity restarts. So I restarted the program and there are no more errors.

now, I can’t assign those public gameobject eyes. The objects from “Hierarchy”(active gameobject) won’t be assigned, while objects from “Project”(assets) will be.

Does StateMachineBehaviour class allow active gameobject?

StateMachineBehaviours should not be able to link objects from the scene. They are a part of the animator controller, which means that whatever you assign to that field will be assigned in all scenes.

In short, the instance of your StateMachineBehaviour lives inside an asset, not inside a scene.

To fix this, you’ll have to have the Eyes linked somewhere else, and get that. You could put this script on the same object as the Animator:

public class EyeLinker : MonoBehaviour {
    public GameObject[] eyes;
}

And then do Smile like this:

public class Smile : StateMachineBehaviour {
	public Texture2D smileTexture;
	private GameObject[] eyes;

	 // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
	override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        if(eyes == null)
            eyes = animator.gameObject.GetComponent<EyeLinker>().eyes;

        ...
	}
}

Of course, if there’s already a script on the Animator, you can add the eyes field there instead.

quiet late but give a try to:

 var temp = animator.GetComponentInParent<DemoAI>();
temp.attackAnimComplete = true;

I think the cleaner way is to use animator.GetBehaviour().OnEnded()+=myCallback
and just invoke in the behaviour the callback