• 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
4
Question by kadamati · Mar 16, 2014 at 10:08 AM · animation controller

How to enable/disable animator by script?

Hi, I have an Animator attached with a gameobject. It has a controller and avatar. I removed the tick beside the animator before pressing the play button, so, it does not automatically plays. Now, while playing the game, if I check on the tick, it plays the desired animation. I want to write a script which receives a keycode (say space bar) and then enables the animator during play time.

How?

Please help.

Comment
Add comment · Show 2
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 kadamati · Mar 16, 2014 at 09:04 AM 0
Share

By the way, I am not sure whether the tag is appropriate here. But this is my very first question. So unity did not allow me to choose my own tag.

avatar image kadamati · Mar 16, 2014 at 08:00 PM 0
Share

I better explain a little more. I have animated a cube explosion in blender and imported it into unity. Now, the imported prefab has thirty-four chunks. Each of the chunks has its own animation inherited from blender. When I add the prefab in my scene, it doesn't automatically starts playing, but when I drag and drop the Default Take clip on the prefab in hierarchy window, and hit play, it plays once. Please note that, no any Animation component has been attached so far. But there is an Animator attached.

The problem is, I cannot control this animation. I attached an Animation (not Animator) component to the prefab (i.e. Component > $$anonymous$$iscellaneous > Animation ), and set the "Default Take" at its "Animation" field, so that i can control it with gameObject.Animation.Play(). But now, unity says,

The AnimationClip 'Default Take' used by the Animation component 'fractureTest' must be marked as Legacy.

alt text

(fractureTest is the name of my prefab) <\br> <\br> Now, I am not going to attach thirty-four animation component separately with all the chunks. It would be extremely ridiculous and time consu$$anonymous$$g. :-(

I am in a fix.

animatorproblem.png (69.4 kB)

6 Replies

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

Answer by xSmurfKasketter · Mar 16, 2014 at 03:10 PM

you can just use the enabled property. i can't remember if it is just called Animator in script, but for example you can do Animation.enabled = true if you want to play it. however i would think that a better way would be to just play it, like Animation.Play() or Animation.Play("animation name") that way it just plays the animation and stops afterwards if it isn't a looped animation.

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 kadamati · Mar 16, 2014 at 08:02 PM 0
Share

Thanks xSmurf$$anonymous$$asketter, but unfortunately it didn't work. Please see the detailed discussion i have just posted. You may perfectly understand my problem now.

avatar image kadamati · Mar 17, 2014 at 04:00 AM 0
Share

Aah! Now it works. Thanks! I saw that particular post before, but didn't quite understand the problem with the rig (i thought I don't have any rig with the mesh! So why bother!? :-p ). Now it's clear. :-D

avatar image
3

Answer by Razouille · Mar 16, 2014 at 02:58 PM

Something like this :

 if (Input.GetKeyDown(KeyCode.Space)) {
     animator.enabled = !animator.enabled;
 }

I guess ?

Comment
Add comment · Show 1 · 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 kadamati · Mar 16, 2014 at 08:03 PM 0
Share

Didn't work, Razouille. :-( Please have a look on the detailed discussion below. I explained more about the problem.

avatar image
1

Answer by xSmurfKasketter · Mar 16, 2014 at 10:46 PM

try this instead then http://answers.unity3d.com/questions/357851/animationclip-must-be-marked-as-legacy.html

Comment
Add comment · 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
1
Wiki

Answer by kadamati · Mar 17, 2014 at 05:34 AM

Though, I still cannot activate the Animator component by scripting, my explosion animation is listening my words by following:

  • Import a .blend file containing multiple objects with animation.

  • In the project window, selected the .blend file, go to Inspector window, click the Rig tab, change the Animation Type to Legecy, then apply the settings.

  • Drag and drop the .blend file to the scene and add an Animation component (Component > Miscellaneous > Animation ).

  • Go to Inspector, drag and drop the Default Take of the .blend file on the component's Animation field.

  • Add a suitable script.

Now codes like gameObject.Animation.Play("Default Take") will work.

Special thanks to xSmurfKasketter for posting forwarding me to this link:

AnimationClip must be marked as Legacy

Comment
Add comment · 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
0

Answer by gamepro5401 · Apr 19, 2015 at 06:19 PM

var scriptToControl;

function Start () { scriptToControl = GetComponent("Animator"); }

function Update() { if (Input.GetKeyDown("w")) { scriptToControl.enabled = true; } } //it Works Perfect

Comment
Add comment · 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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

How to control speed of animation on Unity by a .txt file? 1 Answer

Animation state not transitioning at 100% 1 Answer

How can I reset my idle animation? 1 Answer

Animation begins at start of Gameplay before colliding with trigger 2 Answers

Animations don't work with humanoid avatars 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