UI button triggering a change of scene after an animation has finished (I'm new please help!)

When I press a UI button I need to trigger an animation followed by changing the scene (after the animation has finished). Both of my sets of code work individually, however I am unsure of how to merge them and to ensure the change of scene doesn’t happen until after the animation has finished. I am new to unity so any help would be useful! Thanks :slight_smile:

This is the code I have at the moment to trigger the animation.

public class LeftButtonPress3 : MonoBehaviour {

    public Button Text;
    public Animator ani;
    public Canvas yourcanvas;

    void Start() {
        Text = Text.GetComponent<Button>();
        ani.enabled = false;
        yourcanvas.enabled = true;
    }


    public void Press() {
        Text.enabled = true;
        ani.enabled = true;
        ani.Play("Fisherman - Left 3");
    }
}

This is the code I have at the moment to change the scene.

public class GoToScene1 : MonoBehaviour {
   
    public void ChangeScene(string sceneName) {
        SceneManager.LoadScene(sceneName);
    }
}

Hi,

Then you need to delay the scene change till the clip is done.
One way is to check if the clip is playing, and another is to just wait for the length of the clip in seconds.

For example like this, with a coroutine (haven’t tested the code though).
First the coroutine:

IEnumerator DelayedSceneChange(float delay, string sceneName)
{
    yield return new WaitForSecondsRealtime(delay);
    ChangeScene(sceneName);
}

Then the call to it when the button is pressed:

StartCoroutine(DelayedSceneChange(length_of_clip, sceneName));
1 Like

Thank you, this helped a lot! :slight_smile: