Transition Animation on Button Press?

So I’ve created transition animations between scenes like when opening and closing my main menu from the classic start menu. A black image’s alpha is raised and lowered creating a fade-out effect, so the process is labelled “Fade_Out”. At the same time pressing the “Start Game” button executes the “OpenMenu” variable and immediately switches scenes. How would I go about making the animation play the press of “Start Game” but then transition to the next scene upon it being black?

I’ve already had an attempt at this, but here is the script I’ve been adding onto.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TitleScreen : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    //create variables for UI options
    public void OpenMenu()
    {
        
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void CloseMenu()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);

    }
    public void ExitGame()
    {
        Application.Quit();
        Debug.Log("Game has Quit");
    }
}

This is my addition to said code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TitleScreen : MonoBehaviour
{
    public Animator animator;

    public object StartGame { get; private set; }
    public object QuitGame { get; private set; }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update() {
        if (Input.GetButton(string, StartGame))
        {
            ;
        }

        {
            FadeToMenu(1);
        }
    }

    public void FadeToMenu (int 1)
    {
        animator.SetTrigger("FadeIn");
    }
    //create variables for UI options
    public void OpenMenu()
    {
        
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void CloseMenu()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);

    }
    public void ExitGame()
    {
        Application.Quit();
        Debug.Log("Game has Quit");
    }
}

Any help at all would be incredibly helpful as I want to stop tweaking the UI.

Hi,

In my own projects, I would start a coroutine on button click. The coroutine would handle the button animation and maybe also other effects in this context. When the coroutine is done executing its code, I would call LoadScene.

Since you are working with animations, an animation event at the end of the animation could be an alternative way to solve your problem. Look animation events up in the Unity docs if you don’t know them yet.

Hopefully, this helped. :slight_smile:


See also:

I’ll give it a try. xD

Hihi, so this is what I’ve tried so far lmao

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TitleScreen : MonoBehaviour
{
    public Animator animator;

    public object StartGame { get; private set; }
    public object QuitGame { get; private set; }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update() {
        if (Input.GetButton(string StartGame)) 
        {
            FadeToMenu(1);
        }

    }

    public void FadeToMenu(int BuildIndex + 1)
    {
        animator.SetTrigger("FadeIn");
    }
    //create variables for UI options
    public IEnumerator OpenMenu()
    {
        Animator.Play(string "FadeIn");

        yield return new WaitForSeconds(1f);
        
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void CloseMenu()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);

    }
    public void ExitGame()
    {
        Application.Quit();
        Debug.Log("Game has Quit");
    }
}

I’m gonna look up Animation Events and how to use them so I’ll brb.
Thanks for your help!

Good job! :slight_smile:

Hihi, a weird question but by adding a coroutine there are a few critical errors which I can’t figure out how to solve so it’s not necessarily allowing me to test the animation system without it. It’s only present on Unity not Visual Studio but it’s due to this script change. Could you help? xD

The script above is the one I’ve implemented into the game so you could probably work with that lmao

Thanks again!

Those error messages indicate that there is a typo somewhere in your script. Check the lines where the issue occurs first as the following errors might depend on the first one.

Privacy & Terms