Play Sound When Day/night Cycle Reaches a Certain Hour

Hello,
Here is my script…

var DayLength : float = 2;
var NightLength : float = 1;
var time : float = 2;
var hour : float = 0;
var sun : Light;
var DaySound : AudioClip;
var NightSound : AudioClip;
function Start () {
}
function Update () {
sun.transform.Rotate(Time.deltaTime/time, 0, 0);
hour += Time.deltaTime/time;
if (hour >= 360){
hour = 0;}
if (hour >= 180){
time = NightLength;
// it is night
sun.intensity -= Time.deltaTime;
}
if (hour <= 180){
// it is day
time = DayLength;
sun.intensity += Time.deltaTime;
}
if (sun.intensity >= 1){
sun.intensity = 1;
}
if (sun.intensity <= 0.5){
sun.intensity = 0.5;
}

I’m trying to make it so when the hour is <= 180, then it will play a certain sound, but when the hour is >= 180, then it will stop playing the sound. Same goes for night, if the hour is >= 180 then it will start the Night Sound Var, and if it is <= 180 it will stop playing the Night Sound. If anyone could give me some hints I would really appreciate it.
thanks :stuck_out_tongue:

Add an AudioSource component to your object, and follow something along the lines of the following pseudo code:

public AudioSource DayNightAudio;
public AudioClip DayClip;
public AudioClip NightClip;

public bool PlayingDay = false;

void Start()
{
	DayNightAudio = GetComponent<AudioSource>();
	DayNightAudio.loop = true;
}

void Update()
{
	if (IsDay && !PlayingDay)
	{
		DayNightAudio.Stop();
		DayNightAudio.clip = DayClip;
		DayNightAudio.Play();
		PlayingDay = true;
	}
	else if (IsNight && PlayingDay)
	{
		DayNightAudio.Stop();
		DayNightAudio.clip = NightClip;
		DayNightAudio.Play();
		PlayingDay = false;
	}
}

Make sure that you assign DayClip and NightClip in the inspector for the object.

For more information on the AudioSource and AudioClip types, click here and here respectfully.