UI Button OnClick event missing after changing back from another scene

I have 2 scenes which is a “Level Menu” scene, and the “Level” scene. In the “Level Menu” scene, I have a button in the “Level Menu” which will change the current scene to “Level”. In the OnClick part of that button, I set it to play an audio from an audio source with a DontDestroyOnLoad script attached to it.

The first time the audio would work fine, but after going to the “Level” scene, then going back to the “Level Menu” scene, the button wouldn’t play the audio anymore when I click it.

What I wanted to do is to make the button play a sound, but make it so that the audio doesn’t disappear while changing scenes.

I’ve looked around and I think it’s because the instance is static (?), but I don’t really know what I have to do.

Can anyone help me about this? Thanks!

i am a noob aswell but have you put it under void start?

I am having the same issue. I have an empty game object called LevelManager and it has a DontDestroyOnLoad() attached. It is a static object throughout the scene changes, but during runtime it is missing from the OnClick(). It is present on the first Scene, but when I load a new scene it goes missing and the button no longer works.

HELP would be appreciated.

Old thread, but I think I found a solution. The approach is to

  1. Add the Monobehavior class (with the methods that are meant to be called for the OnClick event) as a component to the game object/button with the Button component.
  2. Drag that script component into OnClick event in the Button component, then use the dropdown and select your method to run.

Additionally, you may need to create a prefab of a game object with just the script attached to it. I didn’t need to instantiate mine in the scene, just attach the script as a component to the same game object with the Button component. Then using the script (drag and drop from the same game object) to the Button’s OnClick. Dragging the script from the project folder does not work.

Hope this helps, I can now switch between scenes without the event disappearing.