Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ryanmillerca · Aug 20, 2018 at 06:52 PM · animatoranimator controllerstatestate-machinestate machine

How to delete a StateMachineBehaviour via Editor Script?

Hi there, I'm building tools to automate creation of complex Animators and would like to be able to remove StateMachineBehaviours on states via script.

I expected that since there is a AddStateMachineBehaviour, there would be a RemoveStateMachineBehaviour, but this does not exist!

Next I tried to get all of the Behaviours on the Animator and call DestroyImmediate on them individually. GetBehaviours works, but DestroyImmediate doesn't appear to do anything.

 CustomStateBehaviour[] customStateBehaviour = myAnimator.GetBehaviours< CustomStateBehaviour>();
 for (int s = 0; s < customStateBehaviour.Length; s++){
     DestroyImmediate(customStateBehaviour[s]);
 }

I also tried putting a Destroy function inside the state machine behaviour and calling that instead of DestroyImmediate.

 public void DestroySelf(){
     DestroyImmediate(this);
 }

This doesn't work either. So how exactly can I destroy many StateMachineBehaviours in an Animator controller via Editor code?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ryanmillerca · Aug 20, 2018 at 07:12 PM 0
Share

Update: Found that GetBehaviours() will return the component itself (the script file) not the instance of it on an State$$anonymous$$achineBehaviour. So, GetBehaviours() is likely the wrong way to get these states.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ryanmillerca · Aug 20, 2018 at 07:32 PM

Figured it out! Needed to get states in a different way, and to set allowDestroyingAssets to true on DestroyImmediate.

 // get animator controller from animator
 UnityEditor.Animations.AnimatorController ac = (UnityEditor.Animations.AnimatorController)myAnimator.runtimeAnimatorController;
 UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[0].stateMachine;
 // iterate through states inside controller
 for (int i = 0; i < sm.states.Length; i++) {
     // pull the state out
     UnityEditor.Animations.ChildAnimatorState childAnimState = sm.states[i];
     // iterate through them
     foreach (StateMachineBehaviour stateMachBehs in childAnimState.state.behaviours){
         // compare the type with the type we want to destroy
         if (stateMachBehs is DestroyThisTypeOfStateBehaviour){
             DestroyImmediate(stateMachBehs, true);
         }
     }
 }
 // needed to see results
 AssetDatabase.Refresh();

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ryanmillerca · Aug 20, 2018 at 07:52 PM 0
Share

This solution gives some odd behaviour (state shows up as invalid/missing until you de-select and re-select the animator and some odd logging stuff), so there may be room for improvement. It's clear that the API isn't interested in accommodating this.

avatar image eelstork · Apr 06, 2021 at 03:13 PM 0
Share

See here https://stackoverflow.com/questions/42789614/unity-how-to-use-editor-script-to-remove-statemachinebehaviour I tried both; I think just reassigning the array is cleaner, as the behaviour refs will be GC'd anyways, and you don't get the inspector trying to display null refs.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Easier way of creating transitions in the animator? 1 Answer

Bool in animator is not changing state 0 Answers

Interface state structure with different type of enemies 0 Answers

Some parts of animation not playing 0 Answers

It is posible to attach a class script, that inherits from MonoBehaviour, to an estate from the state machine? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges