{"id":267,"date":"2022-06-19T16:14:59","date_gmt":"2022-06-19T16:14:59","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=267"},"modified":"2025-07-14T19:41:35","modified_gmt":"2025-07-14T19:41:35","slug":"statemachinebehaviour-t","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/clients\/statemachinebehaviour-t\/","title":{"rendered":"3. StateMachineBehaviour&lt;T&#8230;&gt;"},"content":{"rendered":"<p>Init(args) contains new generic versions of the StateMachineBehaviour base class, extending it with the ability to receive up to six objects during initialization.<\/p>\n<p>For example the following <span style=\"color: #000000;\">AttackBehaviour<\/span> class depends on an object that implements the <span style=\"color: #000000;\">IAttackable<\/span> interface and a ScriptableObject asset of type <span style=\"color: #000000;\">AttackBehaviourSettings<\/span>:<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>class<\/b><\/span> AttackBehaviour <span style=\"color: #808030;\">:<\/span> StateMachineBehaviour<span style=\"color: #808030;\">&lt;<\/span>IAttackable, AttackBehaviourSettings<span style=\"color: #808030;\">&gt;<\/span><\/span><\/pre>\n<p>When you create a component that inherits from one of the generic <span style=\"color: #000000;\">StateMachineBehaviour<\/span> base classes, you\u2019ll always also need to implement the <b>Init<\/b> function for receiving the initialization arguments.<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"><span style=\"color: #800080;\"><span style=\"color: #808030;\"><span style=\"color: #800000;\"><b>public<\/b><\/span><\/span><\/span> IAttackable Target<\/span><span style=\"font-size: small;\"> <span style=\"color: #808030;\">{ <span style=\"color: #800080;\"><span style=\"color: #800000;\"><b>get<\/b><\/span><\/span><\/span><\/span><\/span><\/span>; <span style=\"color: #800080;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"><span style=\"color: #000000;\"><span style=\"color: #808030;\"><span style=\"color: #800000;\"><b>private set<\/b><\/span><\/span><\/span><\/span><\/span><\/span>; <span style=\"color: #000000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"><span style=\"color: #808030;\">}\r\n<\/span><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"><span style=\"color: #800000;\"><b>private <\/b><\/span><span style=\"color: #000000;\">AttackBehaviourSettings settings;<\/span><\/span><\/span><\/span>\r\n<span style=\"color: #800080;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>\r\nprotected override<\/b><\/span><b> <\/b><span style=\"color: #800000;\"><b>void <\/b><\/span>Init<span style=\"color: #808030;\">(<\/span>IAttackable target, AttackBehaviourSettings settings<span style=\"color: #808030;\">)<\/span><\/span>\r\n{<\/span><\/span><\/span>\r\n<span style=\"color: #000000;\">   <span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Target <span style=\"color: #808030;\">=<\/span> target<span style=\"color: #800080;\">;\r\n<\/span><\/span><\/span><\/span><span style=\"color: #000000;\">   this.s<span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">ettings <span style=\"color: #808030;\">=<\/span> settings<span style=\"color: #800080;\">;<\/span><\/span><\/span><\/span>\r\n<span style=\"color: #800080;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">}<\/span><\/span><\/span>\r\n<\/pre>\n<h2>State Machine Behaviour Initializers<\/h2>\n<p>You can use state machine behaviour initializers to specify the Init arguments that are used to initialize an instance of a StateMachineBehaviour&lt;T&#8230;&gt; derived class.<\/p>\n<p><span style=\"color: #000000;\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-271\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2022\/06\/state-machine-behaviour-initializer.png\" alt=\"\" width=\"378\" height=\"158\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2022\/06\/state-machine-behaviour-initializer.png 378w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2022\/06\/state-machine-behaviour-initializer-300x125.png 300w\" sizes=\"(max-width: 378px) 100vw, 378px\" \/><\/span><\/p>\n<p>To achieve this do the following:<\/p>\n<ol>\n<li>Create a state machine behaviour that derives from StateMachineBehaviour&lt;T&#8230;&gt;.<\/li>\n<li>Create an AnimatorController asset.<\/li>\n<li>Add your state machine behaviour to one or more states in the AnimatorController.<\/li>\n<li>Add an Animator component to a GameObject.<\/li>\n<li>Assign the AnimatorController asset to the Animator. After this you should see an Init section inside the Animator component in the Inspector.<\/li>\n<li>Click on the + button in the Init section and select the option to generate an initializer for your state machine behaviour.<\/li>\n<li>Once the initializer class has been generated use the Animator&#8217;s Inspector to specify the initialization arguments.<\/li>\n<\/ol>\n<p>Note that when initialization arguments are provided to a state machine behaviour using an initializer, the arguments will only get injected after the Awake and OnEnable event functions have already finished for the state machine behaviour. If you need to perform one-time setup for a state machine behaviour after they have received their Init arguments, you can either:<\/p>\n<ol>\n<li>Execute your setup function at the beginning of the <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/StateMachineBehaviour.OnStateEnter.html\">OnStateEnter<\/a> function. Use a <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/builtin-types\/bool\">boolean<\/a> variable to keep track of whether or not setup has been performed yet or not.<\/li>\n<li>Execute your setup function at the end of the Init function.<\/li>\n<\/ol>\n<h2 class=\"western\"><span style=\"color: #000000;\"><span style=\"font-family: Liberation Sans, sans-serif;\"><span style=\"font-size: x-large;\"><b>OnAwake<\/b><\/span><\/span><\/span><\/h2>\n<p><span style=\"color: #000000;\">Do not add an Awake function to classes that inherit from one of the generic StateMachineBehaviour classes, because the classes already define an Awake function. If you need to do something during the Awake event, override the OnAwake function instead.<\/span><\/p>\n<h2 class=\"western\"><span style=\"color: #000000;\"><span style=\"font-family: Liberation Sans, sans-serif;\"><span style=\"font-size: x-large;\"><b>OnReset<\/b><\/span><\/span><\/span><\/h2>\n<p><span style=\"color: #000000;\">Do not add a Reset function to classes that inherit from one of the generic StateMachineBehaviour classes, because the classes already define a Reset function. If you need to do something during the Reset event, override the OnReset function instead.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Init(args) contains new generic versions of the StateMachineBehaviour base class, extending it with the ability to receive up to six objects during initialization. For example the following AttackBehaviour class depends on an object that implements the IAttackable interface and a ScriptableObject asset of type AttackBehaviourSettings: public class AttackBehaviour : StateMachineBehaviour&lt;IAttackable, AttackBehaviourSettings&gt; When you create a ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/statemachinebehaviour-t\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/267"}],"collection":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":3,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/267\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}