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 /
This question was closed Mar 07, 2015 at 10:09 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Jan 23, 2015 at 12:13 PM · animationuibuttonstoggle button

UI Button to Toggle Animation and sound

I have been struggling with the new UI system trying to figure out a way to use C# to make a single button toggle an animation on and off with the sound. I currently can get the new UI system to work what I want with two buttons, one button turns off the animation and the other turns it back on but ideally I want to just use one button to toggle the animation and sound on and off. This is the code I have been using for each button to call to. Is there a simple way to do this with just one button?

 using UnityEngine;
 using System.Collections;
 
 public class UIManagerScript : MonoBehaviour {
 
     public Animator LEDlights;
 
 
 
     public void StopLEDLightsAnimation()
     {
         LEDlights.SetBool("isPlaying", true);
 
     }
 
     public void PlayLEDLightsAnimation()
     {
         LEDlights.SetBool("isPlaying", false);
 
     }
 }
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

  • Sort: 
avatar image
1
Best Answer

Answer by sniper43 · Jan 23, 2015 at 01:48 PM

Here's a toggle function. Logical condtions can be passed as bools, so:

 public void PlayLEDLightsAnimation()
 {
     LEDlights.SetBool("isPlaying", !LEDlights.GetBool("isPlaying") );
 }

For anyone who doesn't understand what happens here:

LEDlights.GetBool("isPlaying") returns either true or false. This can be passed as an argument to a function call just like an int or float or GameObject.

The ! at the begining reverses it. It is read as "not", and functions like a logical NOT operation.

!true == false !false == true

Comment
Add comment · Show 4 · 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 · Jan 23, 2015 at 04:10 PM 0
Share

Outstanding!! Thanks for explaining it's function too this helps me too understand the code much better. I think I should be able to do the same thing for the sound associated with the LED lights animation. The sound is on another game object but I think it should work. :)

avatar image KnightRiderGuy · Jan 23, 2015 at 04:36 PM 0
Share

I'm not sure what to call this other function I want to do perhaps you can point me in the right direction. But lets say when I go to another scene and then I come back to this scene naturally the sound and animation by default is playing but if exit out of that scene and then come back in if I have paused the animation and sound it will be playing. I'm wondering if there is a way for those setting to be remembered across scene changes? I have no idea what that game mechanic is called or even if that is possible within Unity?

I'll post this as a separate question if I can think of an appropriate title ;)

avatar image sniper43 · Jan 26, 2015 at 11:14 AM 0
Share

Sorry I didn't respond, I didn't check my mail during th weekend, but I see you've gotten an answer.

avatar image KnightRiderGuy · Jan 26, 2015 at 03:34 PM 0
Share

Yes thai you sniper43 that was perfect and very well explained :)

avatar image
0

Answer by shriya · Jan 23, 2015 at 12:38 PM

Hi,

Follow the below mentioned link

http://www.raywenderlich.com/79031/unity-new-gui-tutorial-part-2

It will help you understanding

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 KnightRiderGuy · Jan 23, 2015 at 01:11 PM 0
Share

That was the tutorial that got me to where I am now and like I mentioned I seem to be able to get it to work with two buttons but ideally I want just one to toggle the animation on and off.

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

How to run 1 animation, then other 2 Answers

can you duplicate unity 4.6 Buttons 0 Answers

Animation not working when UI buttons are pressed 0 Answers

How to make buttons move 3 Answers

Do Something ONLY when all Toggles are On or Off 4 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