- Home /
Toggle between animation states with UI button
Hello there,
I am very new to unity so bear with me
I added subtitles in my scene by putting an animation onto a canvas and use a button Onclick function to toggle the subtitles on/off. I have two animation states in my animator, one where the canvas is 'visible' (subtitles on) and the other where the canvas is 'invisible' ( subtitles off). I added a boolean value between the two animation states transition just so when I click on the button once the subtitles come off and if I click the button again the subtitles are back on. ( which I used in script and added the scrip to the button)
The issue is when the subtitles are back on, the animation is playing from the beginning so the subtitles don't match with what is going on in my scene. I was wondering if there was a way I could switch the subtitles off while still having the animation 'subtitles on' run in the back so when the subtitles are visibly on again in the scene it caught up time wise?
Thank you!
Answer by Insok · Nov 22, 2020 at 09:46 AM
Hi,
You don't need to have 2 different states in the canvas animator for subtitles "on" and "off". You can just play the subtitle "on" animation, and the button could directly hide/unhide the subtitle's renderer without using the animator.
If you aren't sure how to do this, try something like this:
If you use TextMeshPro component
public TMPro.TextMeshProUGUI text;
public void ToggleSubtitles()
{
text.enabled = !text.enabled;
}
If you use default Text component
public Text text;
public void ToggleSubtitles()
{
text.gameObject.SetActive(!text.isActiveAndEnabled);
}
Hello, thanks for yer answer! I actually managed to toggle the subtitles on and off by disabling /enabling the canvas the text was attached to, and it worked like a charm.
Thanks for your help :)