- Home /
How to Play Same Music from an Array on a Different GameObject?
Hey everybody.
I have an array with several pieces of music that plays randomly. What I'm trying to do is play the same song that is currently being played on that game object, on another game object.
So for example. If the current song being played is listed as number 3 in the array, I need the other game object (gameObject A) to also play number 3 and at the same time on (gameObjectB).
Any ideas on how to go about doing this?
Any help is really appreciated.
Thanks!
I can suggest custom events system. There will be some event OnSongPlayed (clip index) You can subscribe any of yours gameObjects for this event, and launch played song after it was played. If it acceptable for you, i can write a code.
Answer by MaT227 · Apr 10, 2014 at 06:11 AM
You can create a music manager that have your music array with audio clips. You can choose you music the way you want in the music manager. Just make your manager instance accessible from other game objects by applying a singleton pattern (pretty easy to find).
Then in your other gameobjects, you can directly access your music using your manager and get your audio clips.
void PlayMusic()
{
AudioClip ac = MusicManager.Instance.CurrentMusic();
audio.clip = ac;
audio.Play();
}
You will need a MusicManager script and MusicPlayer + AudioSource scripts for example.