- Home /
Music track selector
Hi, begginer game maker here. I am currently in the process of adding sound to my game. I have implemented sound effects and songs. For songs I have used an array of songs where they are randomly played from - after one song ends another starts and so on.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicManagerScript : MonoBehaviour {
public AudioClip[] songs;
int songNumber = 0;
AudioSource audio;
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource> ();
//audio.clip = songs [0];
//audio.Play ();
}
// Update is called once per frame
void Update () {
if(audio.isPlaying == false)
{
songNumber++;
if (songNumber >= songs.Length)
songNumber = 0;
audio.clip = songs [songNumber];
audio.Play ();
}
}
}
Now I am trying to make a Menu, where the player could select which song he wants to play. I have a separate scene with buttons and the button text would be song names. Problem is I don't know how to transfer the player's selection to the array. The songs start after the player has clicked Start Game in the Main Menu.
Any help and suggestions would be much appreciated.
Answer by Alanisaac · Jan 06, 2018 at 05:06 PM
One simple way to do this would be to give each button a tag
corresponding to the index of the audio clip in the array. When the player clicks a song choice button, save their selection by reading the tag
from the button and setting a SelectedIndex
property. When the player clicks Start Game, play the clip songs[SelectedIndex]
.
Your answer
![](https://koobas.hobune.stream/wayback/20220612145942im_/https://answers.unity.com/themes/thub/images/avi.jpg)