#Audio issue

1 messages · Page 1 of 1 (latest)

orchid hawk
#

So I just made some scripts for my music and sfx but for some reasons they don't work as intended... As you can see the manager gets the sfx/music manager reference well right when I start the game but once I play a bit and then come back it lost the references and I get missing reference exception error... Please tell me if you would need any other info!

#
using System.Collections.Generic;
using UnityEngine;

public class SFXManager : MonoBehaviour
{
    public static SFXManager Instance;
    [SerializeField] private AudioSource sfxAudioSource;
    [SerializeField] private AudioClip clickClip;
    [SerializeField] private AudioClip playClickClip;
    [SerializeField] private AudioClip deadClip;
    [SerializeField] private AudioClip powerClip;


    public Dictionary<string, AudioClip> soundEffects;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
       

        soundEffects = new Dictionary<string, AudioClip>();
    }
    void Start()
    {
        AddSoundEffect("Click", clickClip);
        AddSoundEffect("Dead", deadClip);
        AddSoundEffect("PlayClick", playClickClip);
        AddSoundEffect("Power", powerClip);
    }

    // Add a new sound effect to the dictionary
    public void AddSoundEffect(string name, AudioClip clip)
    {
        soundEffects[name] = clip;
    }

    // Play a sound effect by its name
    public void PlaySoundEffect(string name)
{
    if (soundEffects.TryGetValue(name, out AudioClip clip))
    {
        sfxAudioSource.PlayOneShot(clip);
    }
    else
    {
        Debug.LogWarning("Sound effect not found: " + name);
    }
}

    public void SetSFXVolume(float volume)
    {
        sfxAudioSource.volume = volume;
    }

}


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

public class MusicManager : MonoBehaviour
{
    public static MusicManager Instance { get; private set; }

    [SerializeField] private AudioSource musicAudioSource;
    [SerializeField] private AudioClip[] musicTracks;
    private int currentTrackIndex = 0;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        PlayNextMusicTrack();
    }

    private void PlayNextMusicTrack()
    {
        // Assign the next music track and play it
        musicAudioSource.clip = musicTracks[currentTrackIndex];
        musicAudioSource.Play();

        // Increment the current track index
        currentTrackIndex++;

        // Check if the index exceeds the length of the music track array
        if (currentTrackIndex >= musicTracks.Length)
        {
            currentTrackIndex = 0; // Reset to loop back to the beginning
        }
    }

    public void SetSFXVolume(float volume)
    {
        musicAudioSource.volume = volume;
    }
}



#

And this is the script from the manager :

simple idol
#

So...
You already went out of your way to do the Singleton pattern for both the SFXManager and MusicManager
Why are you still dragging their reference in the Editor for SettingsEfects?

Just use their respective Instance?

orchid hawk
#

Yeah 😅