Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Adding an existing StateMachineBehaviour to a state

Discussion in 'Animation' started by Manny Calavera, Mar 17, 2015.

  1. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    The following documentation implies I can add an existing StateMachineBehaviour class to a state.. just like I would add an existing MonoBehaviour to different game objects:

    http://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours

    However, when I click the Add Behaviour button, all I see the the 'New Script' option. Dragging an existing script onto the state doesn't work either.

    Am I missing something or was that screenshot just a mock?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    You should see all existing state machine behaviour too just before the New Script option

    smb.png
     
    Manny Calavera likes this.
  3. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Interesting. I believe my animations are suffering from an identity crisis.. They seem to think they're a MonoBehaviour :)

    Notice the following differences:
    - My state behaviour has the Enable/Disable checkbox.
    - The settings menu is different.

    I just created a brand new project and it does behave correctly. So I believe this problem was introduced when I migrated my animation states from 4.5.4 into Beta and now to 5.0.f4.

    I suppose I can work around by switching to debug mode and increasing the array size by hand... although I hope this is just a UI issue that will have no side effects on my animation states :S

    StateBehaviour.PNG
    StateBehaviour_Menu.PNG
     
  4. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Well, I figured out the reason for this weird behavior...

    A long time ago, in the land of Unity 4.5.4 there was no such thing of StateMachineBehaviour.. so I implemented my own logic in a class called, you bet, StateMachineBehaviour which inherited from MonoBehaviour and also had nice little encapsulation of state transition with methods called, you bet again, OnStateEnter(), OnStateUpdate(), etc.

    After importing the project to Unity 5, clicking the button 'Add Behaviour' created a the new class that inherited from StateMachineBehaviour but my StateMachineBehaviour, not Unity's.

    This didn't generate any compilation errors and all state transition methods were being called as expected by 'coincidence' due to the same names and that's how things were working here for the last 3 months with the only side effect being the UI misbehaving..
     
  5. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Well you where lucky that it was working, the life cycle of Monobehaviour and StateMachineBehaviour are really different.

    This is really weird, you didn't have any compilation error on duplicate type name StateMachineBehaviour in c#?
     
  6. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Yes, I understand the asset vs scene object life cycle but no, no compilation errors. I would have expected something like "StateMachineBehaviour is ambiguous between namespace so and so.." but it just 'worked'.

    Silent errors are tough to debug :)

    Also, after fixing the ambiguous name, one small side effect remained: The enable/disable checkbox next to the script name still remains even though the little icon was updated from white C# to blue unity logo (compare with my previous post).

    StateBehaviour_After.PNG
     
  7. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    This is expected, you can disable SMB as you wish, even at runtime you can disable/enable your SMB
     
  8. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    That's true. I was thrown off by your first screenshot that doesn't contain the checkboxes. I get it now, the checkbox will become visible if any of the overrides are present in the code.

    Thanks for helping out. Always good see devs around.
     
  9. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yeah exactly, you need to override at least one of the function otherwise the behaviour does nothing.
     
  10. asd888

    asd888

    Joined:
    Feb 13, 2015
    Posts:
    2
    I have another problem, please do me a favor.
    when i builded a 3d-object prefab into an assetbundle which had "StateMachineBehaviour" attached to it's animator states with unity5.0.3,
    then i load that assetbundle, but the "StateMachineBehaviour" script was not excuted....