hii to all, i need some help cause i need to use a static audiosource but now i dont really know how to assing the audiosource to the script that is going to handle it, any idea about how i could do it? thanks pals ![]()
Hello, what do you mean by “static” audio source exactly?
Like @Magnesium said - what is a static audio source?
If you elaborate on what you’re trying to do we may even offer a better solution!
The most easiest way to do that:
public class AudioSourceInstance : MonoBehaviour
{
[SerializeField] private AudioSource _source;
public static AudioSource Source { get; private set; }
private void Awake()
{
Source = _source;
}
}
//usage:
AudioSourceInstance.Source.Play...
But a bit more advanced thing would be:
public class AudioPlayer : MonoBehaviour
{
[SerializeField] private AudioSource _source;
private static AudioSource _globalSource;
public static void Play(AudioClip clip, float volumeScale = 1f)
{
_globalSource.PlayOneShot(clip, volumeScale);
}
private void Awake()
{
_globalSource = _source;
}
}
//usage:
AudioPlayer.Play(audioClip, 0.8f);
I’d like to know what he means by “static” and what is his precise use. Static variables are pretty much never a good idea.
Hi, thanks all for your help, yea i should have written some code, sorry about that
public class MusicLvl : MonoBehaviour {
public static int efectos = 7;
public static void EffectCntrol()
{
NpcTyp01.VolumeCntrol();
}
}
Public class NpcTyp01 : MonoBehaviour {
public static AudioSource walking;
void Start () {
walking = GetComponent<AudioSource>();
walking.volume = MusicLvl.efectos * 0.1f;
}
public static void VolumeCntrl()
{
walking.volume = MusicLevel.efectos * 0.1f;
}
}
The things is that those npc spawn itselfs when you are near, and it displays this error:
NullReferenceException : Object reference not set to an instance of an object
I think it referes to the AudioSource not beign atached to the npc itself, any idea how can i manage thisÂż thanks for the help ![]()
You can use a singleton, like you are making an audio manager.
Then just give it one audiosource instead of a pool of audiosources.
public class AudioPlayer : MonoBehaviour
{
private static AudioPlayer instance;
public static AudioPlayer Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<AudioPlayer>();
if (instance == null)
{
GameObject audioPlayerGameObject = new GameObject("AudioPlayerGameObject");
instance = audioPlayerGameObject.AddComponent<AudioPlayer>();
instance.AudioSource = audioPlayerGameObject.AddComponent<AudioSource>();
DontDestroyOnLoad(audioPlayerGameObject);
}
}
return instance;
}
}
public AudioSource AudioSource;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
}
Access the AudioSource in your scripts via AudioPlayer.Instance.AudioSource
You probably don’t want just a single audiosource though… which is why everyone is asking you what you’re trying to do.
Well, im trying to manage the volume of the npc effects, and i dont really get what are you doing there, and yea, i have a couple of scripts for each of the npc, and there is cause i have a pool of aufiosource that its managed by a function.
And what doues this script does? like, find a certain tag and add an audioplayer componentÂż
It’s a more robust implementation of a singleton in unity
There are many tutorials on singletons in unity.
If your npcs all need to be able to play their own independent sounds, and their volumes need to be controlled independently because they have different volumes, then you need multiple audiosources.
In my projects I generally don’t use many of them, but where they’re needed (persist across scenes etc), they are perfect for the job. But I suppose there are uses of them that aren’t ideal.
Do you mean about the AudioPlayer¿ i was using diferent scripts for each of them but i was thinking of redo it and use just one script atacched to a certain gameobect inside one of the npcs, and then, apply a tag on them like “sound” and do the trick of find all of them and change the script value, what do you think about it, shall it wotk? or what it is about a single audio source for every one?? or what did you mean?
GameObjects that aren’t destroyed are not exactly singletons, singletons is a design pattern using a static class that instantiates itself when it first is accessed then returns the same instance. GameObjects are instantiated and simply destroy themselves when they find other gameobjects containing the same component. It’s probably a bad design choice but one you have to live with.