Hi all,
I’m still trying to wrap my head around lots of things, but I can’t seem to wrap my head around this: I have several sounds that I want to place in a “SFX” AudioMixerGroup (a child of a master mixer with a single sibling named "Music). I’m trying to manage my audio through a singleton AudoMixerManager class that contains three public fields: one for the master mixer and one each for the child mixer groups. (These are populated within the Inspector.) Looks like this:
using UnityEngine;
using UnityEngine.Audio;
public class AudioMixerManager : Singleton<AudioMixerManager>
{
public AudioMixer m_MasterMixer;
public AudioMixerGroup m_SFXGroup;
public AudioMixerGroup m_MusicGroup;
public AudioMixer MasterMixer
{
get => m_MasterMixer;
}
public AudioMixerGroup SFXGroup
{
get => m_SFXGroup;
}
public AudioMixerGroup MusicGroup
{
get => m_MusicGroup;
}
}
In my player controller script (RocketController.cs), I’m creating my source (for example) like this:
// Create and attach AudioSources and assign clips:
m_MainThrustSource = gameObject.AddComponent<AudioSource>();
m_MainThrustSource.outputAudioMixerGroup = m_AudioMixerManager.SFXGroup;
m_MainThrustSource.clip = m_MainThrustClip;
When I run the game, the bars in the mixer don’t move, and tweaking the volume during play doesn’t change the volume, either. If I try to Debug.Log the outputAudioMixerGroup of the audio source, I get a message saying NullReference. I’m trying to make sure the AudioMixerManager script gets loaded before any scripts attempt to reference the audio mixers, but the console suggests things are trying to reference the audio mixer before it’s ready. I have no idea why I can’t seem to get my audio sources to feed into the mixer. Any advice? Thank you so much!
Hi @Balakay612
It looks like your AudioMixerManager might not have been provided with mixer groups initially, otherwise your object would certainly not return a null reference error if it fetched those values from the manager. You code does not seem to be wrong, but your data might be.
When checking your gameobject at runtime, you should see to what group mixer group they are assigned in the AudioSource inspector. If nothing shows up there, it’s because the value wasn’t updated.
I imagine your Singleton inherits from MonoBehaviour? If it’s the case, you should be able to check at runtime if its data is also properly provisioned.
Let me know what you find. Cheers!
Thanks for taking a look. Yes, my Singleton inherits from MonoBehaviour (it’s based on an example I got from the samyam off YouTube). At runtime, my AudioSource does indeed have no AudioMixer assigned. At this point, I’m not sure what the right approach is to ensure my mixers get assigned properly, nor where to look next. If, at runtime, I manually assign the mixer to the audiosources, of course it works. Thanks for any additional help!
Oh! It’s worth noting that within the editor, I did create an AudioMixer and assign two child mixers to the “master” mixer group that gets created by default. So at design time, three mixers (should?) exist with the AudioMixer: Master, SFX, and Music; SFX and Music are the children of Master (and siblings of one another).
I really just want to be sure that your AudioMixerManager singleton has its mixer and mixergroups set at runtime. If you’re simply spawning the singleton by code and never “feed” him with the proper mixer references, it will never be able to forward them to anyone. Basically, at design time, make sure that your singleton exists somewhere and that it has all it needs to provide non-null references to anyone calling him.
I think I’m setting things correctly at design time: After creating an AudioMixer by right-clicking and choosing Audio Mixer, I then created the two child mixer groups under the Master group. I have a game object to which I’ve attached my AudioMixerManager script (above); in the scene, I plugged the mixer plus the relevant groups (I think?) into the serialized (public) fields exposed through the script. Here’s a screen shot (attached).
Am I missing a step? I realized I never reference the parent of the SFX and Music mixer groups, but my thought is that referencing them through the aforementioned public fields would give Unity enough info to “find” them? Thanks again for the help!
I think everything is alright on this side. The only thing left I’d be looking at is your singleton code. The only reason I see you’d get null references on your AudioMixerManager members would be that the singleton code returns a new instance (with empty members) while disregarding the existence of that current game object.
A clean way to work around this would also be to look into ScriptableObjects. This is a great way to main unique instances of data/code. If you can include the SO in a prefab or reference it on another manager, it’s certainly worth considering it, as a better practice IMO. You might want to look into this.
I’ve read about SOs before, but I’m not terribly familiar with how to implement them. Definitely worth a look: thanks for the link. Here’s what my singleton code looks like:
using UnityEngine;
using UnityEngine.SceneManagement;
public class Singleton<T> : MonoBehaviour where T : Component
{
static T m_Instance;
public static T Instance
{
get
{
if (m_Instance == null)
{
Scene _activeScene = SceneManager.GetActiveScene();
SceneManager.SetActiveScene(SceneManager.GetSceneByName("Managers"));
GameObject _obj = new GameObject();
_obj.name = typeof(T).Name;
_obj.hideFlags = HideFlags.HideAndDontSave;
m_Instance = _obj.AddComponent<T>();
SceneManager.SetActiveScene(_activeScene);
}
return m_Instance;
}
}
void OnDestroy()
{
if (m_Instance == this) m_Instance = null;
}
}
I believe we’ve found the issue. Your singleton does not look for an existing instance of the object on its first call, it’s creating its own.
I’ve been using this for a time, let me know if it fixes your issue.
Cheers!
using UnityEngine;
/* IMPORTANT!!! To use Awake in a derived class you need to do it this way
* protected override void Awake()
* {
* base.Awake();
* //Your code goes here
* }
* */
public abstract class Singleton<T> : MonoBehaviour where T : Component
{
private static T instance;
private static bool m_applicationIsQuitting = false;
public static T GetInstance()
{
if (m_applicationIsQuitting)
{
return null;
}
if (instance == null)
{
instance = FindObjectOfType<T>();
if (instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).Name;
instance = obj.AddComponent<T>();
}
}
return instance;
}
protected virtual void Awake()
{
if (instance == null)
{
instance = this as T;
DontDestroyOnLoad(gameObject);
}
else if (instance != this as T)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
private void OnApplicationQuit()
{
m_applicationIsQuitting = true;
}
}
// Same as above, but without "DontDestroyOnLoad"
public abstract class SceneSingleton<T> : MonoBehaviour where T : Component
{
private static T instance;
private static bool m_applicationIsQuitting = false;
public static T GetInstance()
{
if (m_applicationIsQuitting)
{
return null;
}
if (instance == null)
{
instance = FindObjectOfType<T>();
if (instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).Name;
instance = obj.AddComponent<T>();
}
}
return instance;
}
protected virtual void Awake()
{
if (instance == null)
{
instance = this as T;
}
else if (instance != this as T)
{
Destroy(gameObject);
}
}
private void OnApplicationQuit()
{
m_applicationIsQuitting = true;
}
}
1 Like
Hey! I think that worked! Thank you sooo much! I made some minor modifications to what you provided (shown below) and added the protected override void Awake()
calls to the relevant classes, and it seems like my mixergroups are “connected” at runtime now (the VU-meter-looking bars are dancing!), and testing SetFloat() reduces the volume as I expected it to. I think I have everything I need to hook into the UI stuff I still need to build. Again, thank you so much!
Here’s my tweak to get reference to the scene management stuff I have going on behind everything:
using UnityEngine;
using UnityEngine.SceneManagement;
public abstract class Singleton<T> : MonoBehaviour where T : Component
{
private static T m_Instance;
private static bool m_ApplicationIsQuitting = false;
public static T Instance
{
get
{
if (m_ApplicationIsQuitting) return null;
if (m_Instance == null)
{
m_Instance = FindObjectOfType<T>();
if (m_Instance == null)
{
Scene _activeScene = SceneManager.GetActiveScene();
SceneManager.SetActiveScene(SceneManager.GetSceneByName("Managers"));
GameObject _go = new GameObject();
_go.name = typeof(T).Name;
_go.hideFlags = HideFlags.HideAndDontSave;
m_Instance = _go.AddComponent<T>();
SceneManager.SetActiveScene(_activeScene);
}
}
return m_Instance;
}
}
protected virtual void Awake()
{
if (m_Instance == null)
{
m_Instance = this as T;
DontDestroyOnLoad(gameObject);
}
else if (m_Instance != this as T)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
private void OnApplicationQuit()
{
m_ApplicationIsQuitting = true;
}
}
1 Like