Search code examples
c#buttonaudiounity-game-enginegameobject

Keep Links between two gameObjects when scenes change in unity


I'm Working on a game, and i want to create a mute button. So i wrote a script for that and i attach this script into a gameObject that don't destroy on load.

I link the script with a UI Button present in a main menu. This UI button is destroy when i'm changing scenes.

when i start my game, and i click on the button, the audio turns off, and when i click back, it turns on.

But when i change to another scene, and i go back to the main menu, my script doesn't have the UI Button attach to it. so when i touch the button the audio doesn't change is behavior

I would like to know if it's possible to maintain the link between the UI Button and the script (attach to a normal GameObject), even if the UI Button is destroyed?

i tried this:

ButtonGameObject = GameObject.Find("UI Button");

but it doesn't work.

How can i fix that?

Thanks a lot.


Solution

  • So a better approach rather than having a script search and grab the component is to use a PlayerPrefs class from Unity. Essentially, it will hold onto all important aspects of the game and auto fill information.

    This is a great tool for a lot of user customization aspects of a game. When using this, have a script (sceneController would be a good name) that will run when the scene starts (create a blank object at 0,0,0) and then under void Start() have the script grab the mute/unmute button: GameObject.Find("MuteButton") or (my favorite) give it a tag called MuteButton and run: GameOject.FindWithTag("MuteButton"). Also once you get a link to the button, add a listener to it for when the button is pressed.

    Also store the sound manager in a gameController that will be passed throughout the game. This would control the soundManager and have access to that. So sceneManager will also need a reference to gameManager.

    Using a script that is just for player preferences (a controller if you will) is a better way to organize and contain any preferences for the users. Just better clarity and separation.

    Example

    SceneController Object Script

    class SceneController {
        GameObject muteButton;
    
        void Start() {
            muteButton = GameObject.FindWithTag("muteButton");
            muteButton.AddListener(muteButtonCheck);
        }
    
        void muteButtonClick() {
            if (PlayerPrefs.GetInt("playerMute")) {
                // If 1 (on)
                // Set sound off
                PlayerPref.SetInt("playerMute", 0);
            } else {
                // It's 0 (off)
                // Set sound on
                PlayerPrefs.SetInt("playerMute", 1);
            }
        }
    }