Music playlist script

Updated on April 2, 2015 in  [R] Sound Design
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
1 on March 29, 2015

So i found this script, and correct the little error in it so now the script work.
Basicly that a script that you can use for a music playlist in a game. you can change the song pressing a key you will have choose. You only need to use the script once in an empty scene whit an audio source and the script in the component. It will start the music you have set and it will automatically load the scene named “Menu”

 using UnityEngine;
 using System.Collections;
 public class music : MonoBehaviour
 {
 public AudioClip[] audioFiles;
 public bool DestroyOnLoad = false;
 public KeyCode changeSongKey;
 public int StartUpSong = -1;
 private bool waited = true;
 private int songPlaying = 2;
 // Use this for initialization
 private void Start ()
 {
 if (!DestroyOnLoad)
 DontDestroyOnLoad(gameObject);
 gameObject.AddComponent();
 audio.playOnAwake = false;
 if (StartUpSong != -1)
 {
 audio.clip = audioFiles[StartUpSong];
 audio.Play();
 songPlaying = StartUpSong;
 }
 Application.LoadLevel("Menu");
 }
 // Update is called once per frame
 private void Update ()
 {
 if(!audio.isPlaying && audioFiles.Length > 0)
 {
 int rand = UnityEngine.Random.Range(0,audioFiles.Length);
 audio.clip = audioFiles[rand];
 songPlaying = rand;
 audio.Play();
 }
 if (Input.GetKey(changeSongKey) && waited)
 {
 audio.Stop();
 if (audio.clip == audioFiles[audioFiles.Length-1])
 {
 audio.clip = audioFiles[1];
 songPlaying = 1;
 }
 else
 {
 songPlaying++;
 audio.clip = audioFiles[songPlaying];
 }
 audio.Play();
 waited = false;
 Invoke("waiting" , 0.3f);
 }
 }
 private void waiting()
 {
 waited = true;
 }
 }
 

ps: sorry english is not the first language i learn

  • Liked by
  • Spangui
  • Dion Dokter
Reply
0 on April 2, 2015

Thanks CuttyFlame for collaborating!

  • Liked by
Reply
Cancel