- Home /
The question is answered, right answer was accepted
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);
}
}
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
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. :)
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 ;)
Sorry I didn't respond, I didn't check my mail during th weekend, but I see you've gotten an answer.
Yes thai you sniper43 that was perfect and very well explained :)
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
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
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