- Home /
Day Night Cycle Script Not Working
I have a script that when its 18:00(7:00pm)-7:00(am) the night light should go on which is dark and if its 7:00(am)-18:00(7:00pm) the day light should go on, but when I play the game both of the lights are on D: help!
#pragma strict
var Day : Light;
var Night : Light;
var dt = Date();
var timetext : GUIText;
function Update () {
var day = dt.Now.Day.ToString();
var month = dt.Now.Month.ToString();
var year = dt.Now.Year.ToString();
var hours = dt.Now.Hour.ToString();
var minutes = dt.Now.Minute.ToString();
if (parseInt(minutes) < 10) minutes = "0" + minutes;
var seconds = dt.Now.Second.ToString();
if(parseInt(seconds) < 10) seconds = "0" + seconds;
timetext.text = day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
if(parseInt(hours) > 6)
Day.enabled = true;
Night.enabled = false;
if(parseInt(hours) < 19)
Day.enabled = true;
Night.enabled = false;
if(parseInt(hours) > 17)
Day.enabled = false;
Night.enabled = true;
if(parseInt(hours) < 8)
Day.enabled = false;
Night.enabled = true;
}
Answer by flaviusxvii · Aug 09, 2014 at 04:21 PM
Classic mistake.. and why you should ALWAYS USE BRACES!
if(parseInt(hours) < 8)
Day.enabled = false;
Night.enabled = true;
is the same as
if(parseInt(hours) < 8) {
Day.enabled = false;
}
Night.enabled = true;
Answer by dhandley · Aug 09, 2014 at 06:14 PM
It looks like you're missing curly brackets in your 'if' statements, so the second line in each will always be executed and the Night light will always be enabled. ie:
if(parseInt(hours) > 17)
{
Day.enabled = false;
Night.enabled = true;
}
Answer by Schneider21 · Aug 09, 2014 at 06:14 PM
Are you missing some curly braces on your last set of if statements? It looks like the very last Night.enabled will always be true, since only the Day.enabled = false is being tied to the if check.
Also, it seems a bit inefficient to be doing 4 if statements and all that parsing. Why not save the parsed value to a variable and do it in one if-else?
currentHour = parseInt(hours);
if (currentHour > 6 && currentHour < 19) {
Day.enabled = true;
Night.enabled = false;
} else {
Day.enabled = false;
Night.enabled = true;
}
Your answer
Follow this Question
Related Questions
Day Night Cycle sun fade 1 Answer
How to make a Day/Night Cycle? 2 Answers
Blend two lightmaps for day/night cycle? (terrain) 0 Answers
Object rotation shifting 1 Answer
Day/Nights cycle 2 Answers