Search Unity

Static audiosource - how to use it?

Discussion in 'Scripting' started by Helladah, Apr 11, 2021.

  1. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    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 :)
     
  2. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    179
    Hello, what do you mean by "static" audio source exactly?
     
    Helladah and SparrowGS like this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    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!
     
    Helladah likes this.
  4. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    The most easiest way to do that:
    Code (CSharp):
    1. public class AudioSourceInstance : MonoBehaviour
    2. {
    3.     [SerializeField] private AudioSource _source;
    4.  
    5.     public static AudioSource Source { get; private set; }
    6.  
    7.     private void Awake()
    8.     {
    9.         Source = _source;
    10.     }
    11. }
    12.  
    13. //usage:
    14. AudioSourceInstance.Source.Play...
    15.  
    But a bit more advanced thing would be:
    Code (CSharp):
    1. public class AudioPlayer : MonoBehaviour
    2. {
    3.     [SerializeField] private AudioSource _source;
    4.  
    5.     private static AudioSource _globalSource;
    6.  
    7.     public static void Play(AudioClip clip, float volumeScale = 1f)
    8.     {
    9.         _globalSource.PlayOneShot(clip, volumeScale);
    10.     }
    11.  
    12.     private void Awake()
    13.     {
    14.         _globalSource = _source;
    15.     }
    16. }
    17.  
    18. //usage:
    19. AudioPlayer.Play(audioClip, 0.8f);
    20.  
     
    Helladah likes this.
  5. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    179
    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.
     
    Helladah likes this.
  6. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    Hi, thanks all for your help, yea i should have written some code, sorry about that

    Code (CSharp):
    1. public class MusicLvl : MonoBehaviour {
    2.  
    3. public static int efectos = 7;
    4.  
    5. public static void EffectCntrol()
    6.  
    7. {
    8.  
    9. NpcTyp01.VolumeCntrol();
    10.  
    11.  
    12. }
    13.  
    14.  
    15. }
    16.  

    Code (CSharp):
    1. Public class NpcTyp01 : MonoBehaviour {
    2.  
    3.  
    4. public static AudioSource walking;
    5.  
    6.  
    7. void Start () {
    8.  
    9.  
    10. walking = GetComponent<AudioSource>();
    11. walking.volume = MusicLvl.efectos * 0.1f;
    12.  
    13. }
    14.  
    15. public static void VolumeCntrl()
    16. {
    17.  
    18. walking.volume = MusicLevel.efectos * 0.1f;
    19.  
    20. }
    21.  
    22. }
    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 :)
     
  7. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    You can use a singleton, like you are making an audio manager.
    Then just give it one audiosource instead of a pool of audiosources.

    Code (CSharp):
    1. public class AudioPlayer : MonoBehaviour
    2. {
    3.     private static AudioPlayer instance;
    4.     public static AudioPlayer Instance
    5.     {
    6.         get
    7.         {
    8.             if (instance == null)
    9.             {
    10.                 instance = FindObjectOfType<AudioPlayer>();
    11.                 if (instance == null)
    12.                 {
    13.                     GameObject audioPlayerGameObject = new GameObject("AudioPlayerGameObject");
    14.                     instance = audioPlayerGameObject.AddComponent<AudioPlayer>();
    15.                     instance.AudioSource = audioPlayerGameObject.AddComponent<AudioSource>();
    16.                     DontDestroyOnLoad(audioPlayerGameObject);
    17.                 }
    18.             }
    19.  
    20.             return instance;
    21.         }
    22.     }
    23.  
    24.     public AudioSource AudioSource;
    25.  
    26.     private void Awake()
    27.     {
    28.         if (instance == null)
    29.         {
    30.             instance = this;
    31.             DontDestroyOnLoad(gameObject);
    32.         }
    33.         else
    34.         {
    35.             Destroy(gameObject);
    36.         }
    37.     }
    38. }
    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.
     
    Last edited: Apr 23, 2021
    Helladah likes this.
  8. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    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¿
     
  9. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    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.
     
    Helladah likes this.
  10. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    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.
     
    Helladah likes this.
  11. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    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?
     
  12. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    179
    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.
     
    seejayjames likes this.