- Home /
Day Night Cycle Script Not Working?
I have this script that when its 7-18(7am-6am) its day and the lights and skybox changes and when its 20-5(8pm-5am) the lights change and the skybox. The night time works but when it should be day, its just dawn/dusk skybox and lights, please help!
#pragma strict
var Day_SkyBox:Material;
var Night_SkyBox:Material;
var DawnDusk_SkyBox:Material;
public var Day :GameObject;
public var Night :GameObject;
public var DawnDusk :GameObject;
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;
{
//Day
if(parseInt(hours) > 6 && parseInt(hours) < 19)
{
Day.SetActive(true);
Night.SetActive(false);
DawnDusk.SetActive(false);
RenderSettings.skybox = Day_SkyBox;
}
//Night
if(parseInt(hours) > 19 && parseInt(hours) < 6)
{
Day.SetActive(false);
Night.SetActive(true);
DawnDusk.SetActive(false);
RenderSettings.skybox = Night_SkyBox;
} else {
//Dawn/Dusk
Day.SetActive(false);
Night.SetActive(false);
DawnDusk.SetActive(true);
RenderSettings.skybox = DawnDusk_SkyBox;
}
}
}
Answer by KirieZ · Aug 23, 2014 at 02:01 PM
change: if(parseInt(hours) > 19 && parseInt(hours) < 6)
to: else if(parseInt(hours) > 19 && parseInt(hours) < 6)
if it works, the problem is: You first have checked if it was day, then you check night (even if day routine has run already) and if it isn't night, it'll set to dawn, so when it's day, dawn routine will run too. You should use else if instead of if for night, so only one will run (Day, Night or Dawn)
Hope it helps. Sorry for my bad english
thanks so much! I dont understand too much what else if does but at least it works right now so thanks alot :D
a new problem appeared, night time doesn't work it just goes to dawn/dusk :/
Your answer
Follow this Question
Related Questions
Check what day of the week it is 3 Answers
Screen go Gray and only Cursor active OnTriggerEnter 1 Answer
Times and difference 1 Answer
change gravity after time 1 Answer
Why does the emissive material not work on the surrounding objects? 3 Answers