I am trying to have a script that controls six songs in a daylight
cycle- 3 for night & 3 for day controlled by arrays. I have researched for many
hours and have found nothing.
Here is the script
using UnityEngine;
using System.Collections;
public class MusicGenerator : MonoBehaviour {
void Start ()
{
dayMusic = new AudioClip[3];
playDayMusic();
}
public AudioClip[] dayMusic;
public bool isDay = true;
public AudioClip currentMusic;
// Update is called once per frame
void Update ()
{
if(isDay == true)
{
}
}
void playDayMusic()
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = dayMusic[Random.Range(0, dayMusic.Length)];
audio.Play();
}
}
Help would be great.
I’m kind of new to unity so this may no be the best way, but i had a similar issue, i wanted to cycle multiple background songs randomly one after the other, it should be easy to modify it to factor in day / night.
To do this i had three scripts, one that generated the songs randomly, one that told the generation script when the song was over, and one that was used for publicly accessible variables, i also attached each song i wanted to use onto their own empty prefab in the assets menu. in this example i had three songs.
// this is the variable script, it is attached to an empty game object so that it's a prefab.
using UnityEngine;
public class Variable : MonoBehaviour
{
public bool MusicOn;
}
//this script uses a random number to randomly generate the songs, it is attached to an empty //game object in scene
using UnityEngine;
public class musicGenerator : MonoBehaviour
{
// for each song you need to create a GameObject.
public GameObject music1;
public GameObject music2;
public GameObject music3;
private int whichMusic;
// this part of the script is needed for the publicly accessible variables to work
private Variable variables;
//this is what you attach the variable prefab to
public GameObject Variables;
void Awake()
{
variables = Variables.GetComponent<Variable>();
}
void Start (){
variables.MusicOn = false;
}
void Update (){
whichMusic = Random.Range(1,4);
if(variables.MusicOn == false){
if(whichMusic == 1){
Instantiate(music1);
// this prevents an infinite loop
variables.MusicOn = true;
}
if(whichMusic == 2){
Instantiate(music3);
variables.MusicOn = true;
}
if(whichMusic == 3){
Instantiate(music3);
variables.MusicOn = true;
}
}
}
}
}
// this script uses the date tell when the song started, when it should stop and the current //time, this script needs to be attached to each music prefab e.g. music 1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicControl1 : MonoBehaviour
{
// musicDuration is manually put in and is the duration of the song in seconds
public float musicDuration;
//this is what you attach the variable prefab
private Variable variables;
public GameObject Variables;
System.DateTime musicTimeToReach;
void Awake()
{
variables = Variables.GetComponent<Variable>();
}
void Start(){
musicTimeToReach = System.DateTime.Now;
musicTimeToReach = musicTimeToReach.AddSeconds(musicDuration);
Debug.Log(musicTimeToReach);
}
void Update()
{
System.DateTime myTime = System.DateTime.Now;
Debug.Log(myTime);
if(musicTimeToReach <= myTime){
variables.MusicOn = false;
Destroy(gameObject);
}
if(variables.gameOn == false){
variables.MusicOn = false;
Destroy(gameObject);}
}
}