animation before starting game

so I’m trying to get an explosion animation when I press one of the play buttons and then the game scene starts. I know I probably have to use callback functions, but I have no idea how and I can’t find it online. Under here is the code I think it has to be in.

    public void Start()
    {
        GameObject.FindGameObjectWithTag("gameMusic").GetComponent<MusicClass>().PlayMusic();
    }

    //starts gameMode
    public void PlayGame (int gameMode = 0)
    {
        if (gameMode == 0)
        {
            // enable explosion
            // callback function
            SceneManager.LoadScene("ball");
        }
        if (gameMode == 1)
        {
            SceneManager.LoadScene("reverse");
        }
        if (gameMode == 2)
        {
            SceneManager.LoadScene("all ways");
        }
        if (gameMode == 3)
        {
            SceneManager.LoadScene("1-player");
        }
        if (gameMode == 4)
        {
            SceneManager.LoadScene("breakout");
        }
        if(gameMode == 5)
        {
            SceneManager.LoadScene("start menu");
        }
    }

    //quits gameMode
    public void QuitGame ()
    {
        Debug.Log("Quit");
        Application.Quit();
    }

You mean an explosion (or any kind of animation) WHILE the scene is loading? In that case look into async scene loading. Otherwise you’ll have to make sure to play that animation in the new scene, not the current one.

I want the animation BEFORE you go to another scene. that’s why it’s in the current scene

Then you need to either defer loading the new scene until the animation playback ends, or somewhat more complicated by asynchronuous additive scene loading. See: Unity - Scripting API: SceneManagement.SceneManager.LoadSceneAsync

Async loading with Single loading mode will replace the current scene once loading finished as far as I understand, so there is no guarantee that the animation has finished by this time. With additive loading you can at least hide that scene‘s game objects and set them to active at a later time. But you‘d also have to ensure the current scene will work in companion with additively loaded scenes.

You will need to do these steps in addition to slightly reworking what you have going above:

  • accept the button click to start
  • disable the button’s interactivity (so you can’t double-spam it)
  • kick off your explosion (animation? particle? sounds?)
  • load the scene, but do it by using a “do something later” construct.

I use my CallAfterDelay class for delayed action, anytime I need to “do something later.”

See usage notes at bottom below gist code.

Obviously the delay time interval you choose would be based on how long your explosion goes.

so I’m looking at your code and I am a bit confused what this bit under here means. does it play a bit of music when the button is pressed and then go to the new scene? is ‘ButtonAudioClip’ the music? what does PlayOneShot mean?

AudioSource.PlayOneShot( ButtonAudioClip);
CallAfterDelay.Create( ButtonAudioClip.length, () => {
UnityEngine.SceneManagement.SceneManager.LoadScene( "MyNextScene");
});

Correct.

Correct.

See Unity docs.

Beware you did leave out the optional “disable the button so you can’t spam it” step… but honestly that is probably not super-important!