Search Unity

Audio Having playlist of music play on loop throughout all scenes

Discussion in 'Audio & Video' started by CCouto96, Oct 19, 2018.

  1. CCouto96

    CCouto96

    Joined:
    May 18, 2016
    Posts:
    44
    Hello everyone. I was wondering you could help me with something.

    I have a list of songs that I want to have play on loop throughout all my scenes. I'm having a little trouble getting them to play properly though. I have a script that puts all the songs in an array and another script that loops them. I'm not sure how to get them to work though. I have a game object called "BGMusic" and attached the scripts to that with all of the songs. I am pretty sure I need an audio source but I don't know what to add in terms of an Audio Clip, since I have more than one.

    Any help would be appreciated. I will attach my scripts. Thanks!

    Music Player Script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MusicPlayer : MonoBehaviour {
    6.  
    7.     public AudioClip[] clips;
    8.     private AudioSource audioSource;
    9.     void Start () {
    10.         audioSource = FindObjectOfType <AudioSource>();
    11.         audioSource.loop = false;
    12.     }
    13.    
    14.     private AudioClip GetClip()
    15.     {
    16.         return clips [Random.Range (0, clips.Length)];
    17.     }
    18.  
    19.     void Update () {
    20.         if (!audioSource.isPlaying)
    21.         {
    22.             audioSource.clip = GetClip();
    23.             audioSource.Play();
    24.         }
    25.     }
    26. }
    27.  
    Music Loop Script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MusicLoop : MonoBehaviour {
    6.  
    7.     void Awake ()
    8.     {
    9.         GameObject[] objs = GameObject.FindGameObjectsWithTag ("BGMusic");
    10.         if (objs.Length > 1)
    11.             Destroy (this.gameObject);
    12.  
    13.         DontDestroyOnLoad (this.gameObject);
    14.     }
    15. }
     
  2. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    As long as you're only playing one at a time, make a bunch of different game objects, one for each song. Each has their own Audio Source with its own song. Make a manager script to schedule to play the next when one is just about over.

    Make the whole game object (a parent holder one) DoNotDestroy so it stays on each Scene as you load it.
    If you do additive Scene loading stuff, you may need to move it to the active scene as you disable old Scenes (not positive on that).
     
  3. Colmod

    Colmod

    Joined:
    Jan 9, 2022
    Posts:
    7
    NullReferenceException: Object reference not set to an instance of an object
    MusicPlayerSC.Update () (at Assets/MusicPlayerSC.cs:22)
    pls help
     
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Just add a null check for whatever's being used on that line number
     
    Colmod likes this.