- Home /
How to start/stop/loop different music tracks
Hello,
Thanks for reading. I'm trying to create a script that will allow me to play different types of music, depending on scene and circumstance. For example, at the beginning of a level, when there's dialogue, I'd like to play one type of music, but when the dialogue ends, I'd like to play a different type. I'd like these tracks to loop until they are changed by an external script.
To date, I've just been using audio.PlayOneShot to play SFX, but it doesn't help with looping. Here's the little I've done on this so far, would really appreciate if someone could give me a few pointers with how to proceed.
using UnityEngine;
using System.Collections;
public class JukeBoxScript: MonoBehaviour
{
// FIELDS
// MUSIC
public AudioClip fightMusic1;
public AudioClip fightMusic2;
public AudioClip townMusic1;
public AudioClip dialogueMusic1;
// CLASS METHODS
// Sets the Music to play
public void PlayMusic(string musicTypeIn)
{
// Play the fight music
if(musicTypeIn == "Fight")
{
audio.PlayOneShot(fightMusic1);
}
// Play the Dialogue music
else if(musicTypeIn == "Dialogue")
{
}
// Play the town music
else if(musicTypeIn == "Town")
{
}
}
}
Edit 1: Would it be simpler just to instantiate a gameobject with audio source for each song, set to loop and play on awake, and Instantiate/Destroy the music playing gameobjects as necessary?