Hi,
I have a set of musical sounds I would like to have play randomly and at random intervals as background music. Here’s the code I have so far (attached to the camera):
public void startIEnumPlayMusic(){
StartCoroutine(PlayMusic());
}
private IEnumerator PlayMusic(){
yield return new WaitForSeconds(Random.Range(1, 10));
audio.PlayOneShot(flowerNotes[Random.Range(0, 9)]);
Debug.Log ("play music");
}
This is called from a GUI script attached to a game object using “musicOn” and “musicOff” buttons
if (GUI.Button(new Rect(4, 344, 16, 16), new GUIContent(music_on), B_chooseButtonGUIStyle)){
musicOn = true;
CallPlayMusic();
}
I would like to have the PlayMusic function repeat in a continuous loop while the “musicOn” button is pressed and stop when the “musicOff” button is pressed. I tried setting up a flag in the GUI script: musicOn = false and then I set up a while loop: while (musicOn == true) {CallPlayMusic()}, but unity froze. I would greatly appreciate suggestions. Thanks.
Zaffer