Since the CreateAudioSource(...) method is a general-purpose method that could be used by many different game script classes, it doesn't naturally sit within the MusicManager class. The best place for general-purpose generative methods such as this is to add them as static (class) methods to the component class they work with. In this case, it would be great if we could add this method to the MonoBehavior class itself – so any scripted component could create AudioSource GameObjects on the fly.
All we have to do is create a class (usually named ExtensionMethods) with a static method, as follows:
using UnityEngine; public static class ExtensionMethods { public static AudioSource CreateAudioSource(this MonoBehaviour parent, AudioClip audioClip, bool startPlayingImmediately) { GameObject audioSourceGO = new GameObject("music-player"...