Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by abhishekabz004 · Apr 14, 2015 at 09:12 PM · c#animationblenderruntimeplay

How to go to starting point of an animation which is running already?

Hi. I am kind of a beginner to Unity.

So. an animation(which is a prefab.) is playing in my run time. I want to include a script to this prefab which makes my animation play from starting point on some condition.

Help me in solving this. Or correct me if I had misunderstood things wrongly.

Thanks in advance. :)

Comment
Add comment
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 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Tomer-Barkan · Apr 15, 2015 at 02:22 PM

The animator component has a Play() method that accepts an animation state and also a point in time of the animation in which you want to start from:

You can use this code from a script attached to the same object as the animator:

 string startAnimationState = "StartState";
 Animator animator = gameObject.GetComponent<Animator>();
 animator.Play(startAnimationState, -1, 0);

Assuming the state you want to restart playing is called "StartState" in the animation controller, and that you only have one layer.

Comment
Add comment · Show 6 · 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 abhishekabz004 · Apr 15, 2015 at 05:15 PM 0
Share

Hey. Thanks for replying :) Your code works. But there is a problem. I have multiple game objects instantiated to which this code is to be added.

When the condition gets satisfied, only the first game object gets restarted. That is the first animation which gets instantiated as game object during the run time.

Please help me in solving this. Thanks again for replying. :)

avatar image Tomer-Barkan · Apr 15, 2015 at 05:56 PM 0
Share

And you attached my script too all these objects? Share your entire code, it's hard to figure out what's going on without it.

avatar image abhishekabz004 · Apr 15, 2015 at 07:48 PM 0
Share

Yes. I attached this script of yours to all the prefab(3 different prefabs actually) I want to make that function.

Now, during the run time the same script would be automatically attached to the instantiated game objects.

Here is my code snippet

void Update() { if(condtion is true){ string startAnimationState = "StartState";

Animator animator = gameObject.GetComponent();

animator.Play(startAnimationState, -1, 0); } }

And these animators each have separate starting states named StartState.

Thanks in advance! Cheers :)

avatar image Tomer-Barkan · Apr 15, 2015 at 08:12 PM 0
Share

As long as they're all enabled, all have an animator with a state called StartState, and the condition is true for all of them, I see no reason why it shouldn't work. Look deeper and try to find what you did wrong.

avatar image Tomer-Barkan · Apr 16, 2015 at 04:28 PM 1
Share

The code you posted is just fine. There is a bug somewhere in your other code, you'll have to do some debugging to find it, we can't debug it for you. You can use a debugger or Debug.Log to print the condition values just before the if, see if the code gets executed at all and if not try to figure out why.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animation doesn't play 1 Answer

Unity won't allow animations 1 Answer

How to set component property value at runtime not having components source? 0 Answers

How do I stop my animation from looping while I hold down a key? 1 Answer

How can i put multiple animations on one object in blender? 1 Answer


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