Search Unity

Timed Events Such As Holidays?

Discussion in 'Scripting' started by Studio_Akiba, Dec 14, 2014.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    If I were to place certain objects such as christmas lights on a building (or a tree) and put them all under a parent gameobject and turn that object off, in script is it possible to set up a timer so only in December will it show up?

    I would like to do this for a variety of things like menu text and other models and effects, if this is possible could someone assist in setting this up?
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You can use functions provided by .Net ; System.DateTime.Now, for example.
     
    Mycroft likes this.
  3. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    This looks kinda like what we are looking for, but I'm not sure how to implement it, I need to provide 2 dates and have it only active when the date is between the two set
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
  5. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    that doesn't really help much, it seems very enigmatic to me, I am not used to doing things with the system in code.
     
  6. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    Seems hard to test, you have to wait for the appropriate dates lol
     
  7. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    I don't understand why you think this is hard..

    Code (CSharp):
    1.         public DateTime christmasTime = new DateTime(DateTime.Now.Year, 12, 25);
    2.         public DateTime easterTime = new DateTime(DateTime.Now.Year, 4, 20);
    3.  
    4. void Start() {
    5.             DateTime startChrismas = christmasTime.AddDays(-10);
    6.             DateTime endChrismas = christmasTime.AddDays(10);
    7.  
    8.             myChristmasGameObjects.SetActive(DateTime.Now.Ticks > startChrismas.Ticks && DateTime.Now.Ticks < endChrismas.Ticks);
    9. }
    ...
     
    hopeful likes this.
  8. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Thanks so much, it works, just not sure how.

    What are the numbers?
    I am guessing 12 is the month and 25 being the day, but easter isn't on the 20th of April, and what are the -10 and 10 things?
     
  9. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Documentation DateTime. So yes, 12 is the month and 25 the day. There are other constructors available.

    Easter WAS on the 20th April 2014 : List of dates for Easter.

    About AddDays, like it says, it add days depending on the DateTime variable. I did that because I think you wanna show the Christmas things for several days, not for only one day. It means "Active all christmas gameObjects from 15th December 2014 to 4th January 2015". It's just an example, but you can do whatever you want.
     
  10. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Ahh, sorry I'm still thinking Easter is in March :oops:

    Thanks for explaining how it all works, makes much more sense now, thanks :D