#We don t have enough info on your setup

1 messages · Page 1 of 1 (latest)

spiral arch
#
public class DoNotDestroy : MonoBehaviour
{
    private AudioSource _audioSource;
    public static DoNotDestroy instance;

    private void Awake()
    {
        GameObject[] music = GameObject.FindGameObjectsWithTag("Music");
        if (music.Length > 1)
            Destroy(gameObject);
        instance = this;
        DontDestroyOnLoad(gameObject);
        _audioSource = GetComponent<AudioSource>();
    }
    public void PlayMusic()
    {
        if (_audioSource.isPlaying) return;
        _audioSource.Play();
    }
    public void StopMusic()
    {
        _audioSource.Stop();
    }
}
#

Do Not destroy script

#
    public void Victory()
    {
        DoNotDestroy.instance.PlayMusic();
        animator.SetTrigger("Victory");
        stop = true;
    }
``` Where its referenced
#

here's it on DDOL when the scenes first loaded

#

It's the exact same when scenes are reloaded, but It won't let me call it

storm heath
spiral arch
#

"MissingReferenceException: The object of type 'AudioSource' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object."

#

except

#

it isnt destroyed

storm heath
#

Ok. Your Singleton pattern is incomplete. Your victory music objects will keep on duplicating in each scene reload. You need to check for Singleton existence and either destroy the old instance or the new one. Look up singletons in unity to understand more.