Search Unity

Turn lights off at night time?

Discussion in 'Scripting' started by Sorrowthief, Jan 7, 2017.

  1. Sorrowthief

    Sorrowthief

    Joined:
    Dec 14, 2016
    Posts:
    48
    I have looked all over but cant find any answers to this. I use a Day and night cycle and one thing that really bothers me is that some of my lights need to go off at night time and others need to go off at day time. For example I have a spotlight attached to lamp post outside of my gas station. When its 12 noon and sun blaring this light needs to go off. Is there a way to do this? Would I have to work a script in conjuction with my day and night cycle? Thanks for any replies.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You would need a script, that gets the "time" from the daynightcycle script, and turn off/on the lamps according to that
     
  3. Deleted User

    Deleted User

    Guest

    Following @gorbit99 advice, you'd probably have to write the DayNightCycle script too. :)
     
  4. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    Yes, you would need some script that controls the lights.
    One thing I have done, is use events on my simple time system (which simply determines what time it is in the game, not the actual day/night cycle).
    I setup events for when any part of the time changes, such as OnSecondChange, OnMinuteChange, and OnHourChange.
    With a setup like this, if I wanted to have lights turn on/off at different times, I could have my light scripts access my time script, and listen for when the hour changes. Then the light would compare the new hour, and determine if it should be on or off.

    If you would like to learn more about events, you can watch these two short videos:
    Unity - Delegates

    Unity - Events