dissertation / mainCode / UI / MusicPlayer.cs
MusicPlayer.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MusicPlayer : MonoBehaviour
{

    public AudioClip song1, song2, song3;
    public AudioSource player;
    private int lastSong = 0;
    public static MusicPlayer musicPlayer;
    
    void Awake () {
        DontDestroyOnLoad (transform.gameObject);
        if (musicPlayer == null)
        {
            musicPlayer = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }
    
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<AudioSource>();

    }

    // Update is called once per frame
    void Update()
    {
        if (!player.isPlaying)
        {
            var clips = new List<AudioClip>{song1, song2, song3};
            int song = Random.Range(0, 3);
            player.clip = clips[song];
            player.Play();
        }

        player.volume = PlayerPrefs.GetFloat("audioVolume");
    }
}