UI Button Script doesn't work after scene reloading (672919)

Hi! UI buttons stop working after I load another scene and then come back. I check where is the problem, and i noticed that the script (which is assign to the button) wasn’t there anymore. I attached some screens to clarify the things.
Thank you in advance!

Can someone help me, please?

is the networkmanager still in the scene when you come back?

Nope. But when I press play, unity is creating a temporary scene called “DontDestoryOnLoad” which contains only the networkmanager

Do you know where is the problem?

Can I get help, please? :slight_smile:

are following some tutorial for this menu system?

Yes…I am pretty sure that I didn’t miss any line of code.

https://www.youtube.com/watch?v=V4oRs26vAw8

This is the episode where he is doing the host script and attach it to the networkManager…

I have 3 scene in the project now… LoginMenu, Lobby and MainLevel. The game starts with “LoginMenu”. Before I play the project, networkManager is there with the script associate. So…I play the game, “LoginMenu” is starting , after i enter the username and the pass, and hit ‘Login’, my project will change the scene to “Lobby” where I can create or join a room. Buuut, the networkManager isn’t there anymore. So…The networkManager is on the scene only when I start the project from the “Lobby” scene…And then, when I come back to the “Lobby” scene, networkManager is not there anymore…whoaa… :frowning:

Did you come up with solution? I am having the same problem after coming back to Menu with NetworkManager…

It has been along time since then…But… I think I made a script which keep the object (with the button script) to ‘dont destroy on load’ and put that object (with script button) on EVERY SCENE.

Tried all of these and didn’t fix it my issue. Finally found my problem:

I’m using the standard 1st person controller asset, and it locks the cursor. Need to unlock the cursor when going back to the main menu.

In the past I have had:

  1. UI textbox/image is on top of button so button isn’t clickable
  2. DontDestroyOnLoad doing wonky things

Cheers

I have the same problem exactly. The game objects that contain the functions added to OnClick() in the buttons are set “DontDestroyOnLoad”. I don’t understand why OnClick() is empty after going back to the scene.

I just discovered that putting these script DontDestroyOnLoad was the reason for the buttons On Click() to be empty after the scene has been reloaded.

If, like me, you need to have one of those scripts to be DontDestroyOnLoad and these scripts have functions that are destined to be used in On Click() events, you’ll have to use a script to set the On Click event instead of using the ready made setting on the button. You can also avoid putting these functions in DontDestroyOnLoad scripts, of course. The script would look like this:

using UnityEngine;
using UnityEngine.UI;

public class ButtonOnClickEvent : MonoBehaviour
{
    private Button button;
    private Script script;

    private void Awake()
    {
        button = GetComponent<Button>();
        script = GameObject.Find("Script").GetComponent<Script>();

        button.onClick.AddListener(OnClickEvent);
    }

    private void OnClickEvent()
    {
        script.function();
    }
}
1 Like