Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by KnightRiderGuy · Feb 02, 2015 at 05:38 PM · animationuianimationclipbutton trigger eventsanimationstate

Make Animation Loop X Times C#

I am currently using this method with a UI button to play and stop my animation on a GUI element. It works but not quite exactly what I want. I would like to try and make it so that the animation plays "X" amount of times when the button is pressed. I think a Trigger is what I need? This is how I'm currently doing it with a Bool.

     public void PlayAnalyzerLightsAnimation()
         {
             Analyzerlights.SetBool("AnalyzerLightsOff", !Analyzerlights.GetBool("AnalyzerLightsOff") );
         }

Comment
Add comment · Show 4
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 Mmmpies · Feb 02, 2015 at 07:23 PM 0
Share

Not being critical but use the code button when you put code, makes it more readable and, therefore, more likely to get an answer. It's the

101 010

button. Don't worry I've done for you this time.

I'm not sure if this will help but if you know how long the animation takes could you set a float to Time.time + (AnimLength * 10) and to follow my own example:

 private float AnimLength = 1.2f;
 private float PlayTen = 0f;
 
 public void PlayAnalyzerLightsAnimation()
 {
     bool tempBool = Analyzerlights.GetBool("AnalyzerLightsOff");
     Analyzerlights.SetBool("AnalyzerLightsOff", !tempBool);
 
     if(!tempBool)
         PlayTen = Time.time + (AnimLength * 10);
 }
 
 void Update()
 {
     if(Time.time >= PlayTen && PlayTen != 0f)
     {
         PlayerAnalyzerLightsAnimation();
         PlayTen = 0f;
     }

Although not tested that code.

avatar image KnightRiderGuy · Feb 02, 2015 at 08:10 PM 0
Share

It gives me the following error message: Assets/Scripts/Analyzer$$anonymous$$anagerScript.cs(127,33): error CS0103: The name `PlayerAnalyzerLightsAnimation' does not exist in the current context

avatar image KnightRiderGuy · Feb 02, 2015 at 08:15 PM 0
Share

Never $$anonymous$$d I figured out what it was it was supposed to say Play ins$$anonymous$$d of player ;)

avatar image Mmmpies · Feb 02, 2015 at 08:19 PM 0
Share

Good and don't forget that 1.2f is just an example. You'll need to replace it with the time your animation takes to play.

1 typo in untested code is pretty good considering how bad my typing is, sorry I'd normally test code before posting but just went in to fix your code layout and got carried away!

2 Replies

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

Answer by Mmmpies · Feb 02, 2015 at 08:14 PM

Looks like a typo, I only went in to change you code section and got carried away try this:

 private float AnimLength = 1.2f;
  private float PlayTen = 0f;
  
  public void PlayAnalyzerLightsAnimation()
  {
      bool tempBool = Analyzerlights.GetBool("AnalyzerLightsOff");
      Analyzerlights.SetBool("AnalyzerLightsOff", !tempBool);
  
      if(!tempBool)
          PlayTen = Time.time + (AnimLength * 10);
  }
  
  void Update()
  {
      if(Time.time >= PlayTen && PlayTen != 0f)
      {
          PlayAnalyzerLightsAnimation();
          PlayTen = 0f;
      }

Pluralized the PlayAnalyzerAnimation(); line

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 KnightRiderGuy · Feb 02, 2015 at 08:23 PM 0
Share

Thanks $$anonymous$$mmpies, I had already made the fix but this works ;)

avatar image Mmmpies · Feb 02, 2015 at 08:33 PM 0
Share

Happy to help @zentaiguy :¬)

avatar image
0

Answer by osadst · Jun 03, 2017 at 11:32 AM

@zentaiguy @Mmmpies

Hi guys, I am trying to modify sthe olution above to be able to play my animation clip X times after pressing the button and going back to Idle animation after that. I am graphic designer, not a programmer and need to make it for my work placement. I'd highly appreciate any help.

cheers guys!

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

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

21 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

Related Questions

I Need The Help Or Advice Of A Unity Animation Expert 0 Answers

How can a start an animation by clicking on a button, then with that same button, start a different animation the second time you click on it. 1 Answer

Moving Player only during part of an animation 1 Answer

Trying to manage an Animation. AnimationState.time doesn't adjust the animation 1 Answer

Simple state switching to mouse over & pressed requires 2 animation clips with some parts duplicated? 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