Help please my script stopt working and i don't now why

Hello my script was working before but now when i go back to where the script is the background (bg) audio does not stop playing (its starts a new song while the other one is still playing) before it deleted the dontdestroyonload but now it does not do that anymore. can you help me pls

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

public class BgSound : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    //Play Global
    private static BgSound instance = null;
    public static BgSound Instance
    {
        get { return instance; }
    }

    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);
    }
    //Play Gobal End

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

    }
}

The above code is literally nothing but a Unity singleton. It does nothing. Any audio you thought was coming from this code was actually coming from somewhere else.

Not sure where your other code is, but perhaps start by liberally sprinkling Debug.Log() statements around to make sure the code is even running.

2 Likes

I feel so stupid now that there was an audio source on my main menu that i left there for a test

1 Like

You’re not stupid, don’t worry. I do this sort of thing all the time.

Honestly only about 10% of Unity is coding, the rest of it is “Where did I put that silly thing in my scene?!”

Glad you got it working. :slight_smile:

2 Likes