Search Unity

Question audio not Playing While Changing the scene

Discussion in 'Audio & Video' started by unity_Wi_c3HS4zMBBEA, Apr 3, 2021.

  1. unity_Wi_c3HS4zMBBEA

    unity_Wi_c3HS4zMBBEA

    Joined:
    Nov 22, 2020
    Posts:
    1
    Hi.
    My problem is that when I switch from the Menu Scene To the game Scene The Background Music Doesn't Play While The Function Gets Called The Way It Should.
    All the Other Sounds Like Impact and... are played Correctly.
    Here is my Audio Manager Code:
    Code (CSharp):
    1. public class AudioManager : MonoBehaviour
    2. {
    3.     public  Sound[] sounds;
    4.     public  static AudioManager AudioInstance;
    5.     private AudioSource[]       allAudioSources;
    6.     private void Awake()
    7.     {
    8.        
    9.         if(AudioInstance == null)
    10.             AudioInstance = this;
    11.         else
    12.         {
    13.             Destroy(gameObject);
    14.             return;
    15.         }
    16.         DontDestroyOnLoad(gameObject);
    17.         foreach(Sound s in sounds)
    18.         {
    19.             s.source = gameObject.AddComponent<AudioSource>();
    20.             s.source.clip = s.clip;
    21.             s.source.volume = s.volume;
    22.             s.source.pitch = s.pitch;
    23.             s.source.loop = s.loop;
    24.             s.source.priority = s.priority;
    25.         }  
    26.     }
    27.  
    28.     public void Play(string name)
    29.     {
    30.         //Debug.Log(name);
    31.         Sound s = Array.Find(sounds , sound => sound.name == name);
    32.         if(s == null)
    33.         {
    34.              //Debug.Log(name);
    35.             return;
    36.         }
    37.         s.source.Play();
    38.     }
    39.  
    40.     public void Stop(string name)
    41.     {
    42.         //Debug.Log(name);
    43.         Sound s = Array.Find(sounds , sound => sound.name == name);
    44.         if(s == null)
    45.         {
    46.             return;
    47.         }
    48.         s.source.Stop();
    49.     }
    50.  
    51.     //is called when u die and stop all the playing sounds
    52.     public void StopAllSounds()
    53.     {
    54.         //Debug.Log("stoping");
    55.         allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
    56.         foreach( AudioSource audioS in allAudioSources)
    57.         {
    58.             audioS.Stop();
    59.         }
    60.     }
    61. }
    62.  
    and this is the error I get when I click the play button and change the scene:
    ArgumentNullException: Value cannot be null.
    Parameter name: source
    UnityEngine.AudioSource.Play () (at <26ff2a7b056a476b973627db643ee7d8>:0)
    AudioManager.Play (System.String name) (at Assets/Scripts/Control scripts/AudioManager.cs:41)
    PlayerMove.Start () (at Assets/Scripts/moving scripts/PlayerMove.cs:28)


    I'm using unity 2020.1.0f1