Legacy Documentation: Version 5.0
Language: English
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

AnimatorController.CreateStateMachineBehaviour

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public static function CreateStateMachineBehaviour(script: MonoScript): int;
public static int CreateStateMachineBehaviour(MonoScript script);

Parameters

scriptMonoScript class to instantiate.

Returns

int Returns instance id of created object, returns 0 if something is not valid.

Description

This function will create a StateMachineBehaviour instance based on the class define in this script.

This function will validate that the monoscript is a valid statemachine behaviour, the class must be a sub class of StateMachineBehaviour and shouldn't be a generic. See Also: UnityEngine.StateMachineBehaviour.

#pragma strict
class AddSMB {
	public function DoAddStateMachineBehaviour(state, monoScript) {
		if (state == null)return ;
		var instanceID = AnimatorController.CreateStateMachineBehaviour(monoScript);
		if (instanceID == 0) {
			Debug.LogError("Could not create state machine behaviour " + monoScript.name);
			return ;
		}
		state.AddBehaviour(instanceID);
		var obj = EditorUtility.InstanceIDToObject(id);
		if (obj == null)
			Debug.LogError("No object could be found with instance id: " + id);
		else
			AssetDatabase.AddObjectToAsset(obj, state);
	}
}
class AddSMB
{
	public void DoAddStateMachineBehaviour(UnityEditor.Animations.AnimatorState state, MonoScript monoScript)
	{
		if (state == null)
			return;

int instanceID = AnimatorController.CreateStateMachineBehaviour(monoScript); if (instanceID == 0) { Debug.LogError("Could not create state machine behaviour " + monoScript.name); return; }

state.AddBehaviour(instanceID);

var obj = EditorUtility.InstanceIDToObject(id); if(obj == null) Debug.LogError("No object could be found with instance id: " + id); else AssetDatabase.AddObjectToAsset(obj, state); } }