Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 eyaltal1 · Dec 30, 2015 at 04:29 AM · animationbuttonclick

How To Play Animation On UI Button Click Please... Im Going To Freak Out Please Enter And Help Me

Its Have Been Days That I Looked For The Answers... I Just Want To Play An Simple Animation When Im Clicking My UI Button Iv Got An Script And Im Clicking My Button Its Playing The Animation... But When Im Clicking On It Agian And Nothing Happend. Please Help Me How To Figure It Out. Script:

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class play_anim_on_ui_button : MonoBehaviour {

 public Button Text;
 public Animator ani;


 void Start () 
 {
     ani.enabled = false;
 }

 public void Press() 

 {
     ani.enabled = true;

 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by isablue27 · Jul 12, 2017 at 03:14 AM

Create a new c# script and paste this code. add the script to the object that own the animation

//Script Made By StrupsGames// using UnityEngine; using UnityEngine.UI; using System.Collections;

public class play_anim_on_ui_button : MonoBehaviour {

 public Button Text;
 public AudioClip sound;
 public Animator ani;
 public Canvas yourcanvas;
  
  
  
 void Start () 
 {
     Text = Text.GetComponent<Button> ();
     ani.enabled = false;
     yourcanvas.enabled = true;
 }
 
  
 public void Press() 
      
 {
     Text.enabled = true;
     AudioSource.PlayClipAtPoint(sound, transform.position);
     ani.enabled = true;
     
 
 }

}

then select the object on the hierachy, go to the inspector, you are going to see this link text

fill it

then go to your UI button and in the inspector, on click() , add to list, and http://imgur.com/V4M3lSR

this function will enable our animator, so the fist time we click out button, the object is gonna be animated but the second time we click it, it wont, to make this happen, "add to list" other function and alt text

once you did that, in the function choose : alt text

now every time you click the button this function will play the animation

if you want to add sound to your running animation you just need to create an audio source and in the inspector add the audio clip http://imgur.com/xOtHf5V

then go to the button inspector and add a new function, "add to list" drag the object audio source you created and you will have to choose the audio clip again http://imgur.com/j4DkHnR

now just select audio source function, playoneshot(Audioclip)

http://imgur.com/sE9x61Y

This way every time you click it will reproduce the animation and the sound at the same time

i hope beeing helpful


select-animator-object.jpg (145.6 kB)
animatorplay.png (73.3 kB)
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 Cornelis-de-Jager · Jul 12, 2017 at 03:24 AM 0
Share

You know man, detailed answer and all, but the reason I an Voting you up is because you replied to a question asked in 2015. THats some serious dedication.

avatar image
2

Answer by allenallenallen · Dec 30, 2015 at 04:31 AM

All you did every time you press is enable the animator. That's it. When the animator is enabled, it plays the default animation once and only once.

Use this: http://docs.unity3d.com/ScriptReference/Animation.Play.html

Play the animation after enabling the animator each time you press the button.

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

7 People are following this question.

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

Related Questions

Playing different button animation after first animation plays 1 Answer

How can I stop an animator playing 0 Answers

mouse clicked on object animation c# 1 Answer

Unity 4.6 UI Button Click Animation? 4 Answers

Play animation when in collider and button pressed 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