Help with my Events script

Since using my Events script (detect the days of things like Christmas) I have fallen in love with it, brilliant little feature.
But I think the time has come to make it a little more robust and dynamic.
I started by putting everything I needed into a serialized class (you may have noticed me using them A LOT).
Then I realised that as DateTime is not a regular variable, it cannot be called in the same way as it does not appear in the editor, so I changed it to work with simple integers, which I think works quite well.

My main problem now is that I cannot limit the Days and Months to specific ranges using Mathf.Clamp, take a look, posted below.

The first one is the original, and the one below that is the new Work In Progress one.

using UnityEngine;
using System.Collections;
using System;

public class EventObjects : MonoBehaviour {

    public GameObject ChristmasObjects;

    public GameObject NewYearsObjects;

    //******************
    //**Variable Dates**
    //******************

    public DateTime christmasTime = new DateTime(DateTime.Now.Year, 12, 25);
    public DateTime easterTime = new DateTime(DateTime.Now.Year, 4, 20);
    public DateTime newYearTime = new DateTime (DateTime.Now.Year, 1, 1);
   
    void Start() {

        //***********************************
        //**Variable Date StartEnd Settings**
        //***********************************

        DateTime startChrismas = christmasTime.AddDays(-10);
        DateTime endChrismas = christmasTime.AddDays(10);


        DateTime startNewYear = newYearTime.AddDays (-1);
        DateTime endNewYear = newYearTime.AddDays (0);

        //*************************
        //**GameObject Activation**
        //*************************

        ChristmasObjects.SetActive(DateTime.Now.Ticks > startChrismas.Ticks && DateTime.Now.Ticks < endChrismas.Ticks);

        NewYearsObjects.SetActive (DateTime.Now.Ticks > startNewYear.Ticks && DateTime.Now.Ticks < endNewYear.Ticks);

    }
}
using UnityEngine;
using System.Collections;
using System;

[System.Serializable]
public class EventsArray : System.Object
{
    [Tooltip("This is the name of the event.")]
    public string EventName;
    [Tooltip("This is the gameobject(s) enabled on the event.")]
    public GameObject EventObjects;
    [Header("Event Date")]
    [Tooltip("The Month of the event.")]
    public int EventMonth;
    [Tooltip("The Day of the event.")]
    public int EventDay;
    [Tooltip("The amount of days before the Event Day to start the event.")]
    public int DaysBefore;
    [Tooltip("The amount of days after the Event Day to continue the event.")]
    public int DaysAfter;
}

public class EventSystem : MonoBehaviour
{

    [Header("Events Manager")]

    public EventsArray[] EventsList;

    public void Update()
    {
        for (int i = 0; i < EventsList.Length; i++)
        {
            EventsList[i].EventMonth = Mathf.Clamp(EventsList[i].EventMonth, 0, 12);
            EventsList[i].EventDay = Mathf.Clamp(EventsList[i].EventDay, 1, 31);
        }
    }
}

You can use ExecuteInEditMode to allow your script to run in the editor. This means you can take full advantage of DateTime.

(Assuming that would help)

Ah, this worked, thanks.
I couldn’t find this, but I guess I was searching the wrong thing.

Edit: I was hoping to use something like this:

    public DateTime EventTIme = new DateTime(DateTime.Now.Year, EventMonth, EventDay);

Inside my serialized class but I get errors thrown which has something to do with using integers in DateTime (I think):

(22,65): error CS0236: A field initializer cannot reference the nonstatic field, method, or property EventsArray.EventMonth' (22,77): error CS0236: A field initializer cannot reference the nonstatic field, method, or property EventsArray.EventDay’
(22,86): error CS1502: The best overloaded method match for System.DateTime.DateTime(int, int, int)' has some invalid arguments (22,86): error CS1503: Argument #2’ cannot convert object' expression to type int’

Looks like it has a problem with your EventMonth and EventDay.

I don’t think you can use the values of variables within the same declaration.

Ex:
class whatever{
int bob = 5;
int steve = bob;
void Start(){}
}

Try doing this approach instead:

public DateTime EventTIme;

void Start(){
    EventTIme = new DateTime(DateTime.Now.Year, EventMonth, EventDay);
}

Now using this:

using UnityEngine;
using System.Collections;
using System;

[System.Serializable]
public class EventsArray : System.Object
{
    [Tooltip("This is the name of the event.")]
    public string EventName;
    [Tooltip("This is the gameobject(s) enabled on the event.")]
    public GameObject EventObjects;
    [Header("Event Date")]
    [Tooltip("The Month of the event.")]
    public int EventMonth;
    [Tooltip("The Day of the event.")]
    public int EventDay;
    [Tooltip("The amount of days before the Event Day to start the event.")]
    public int DaysBefore;
    [Tooltip("The amount of days after the Event Day to continue the event.")]
    public int DaysAfter;
}
[ExecuteInEditMode]
public class EventSystem : MonoBehaviour
{
    public DateTime EventTime;

    [Header("Events Manager")]

    public EventsArray[] EventsList;

    public void Start()
    {
        EventTime = new DateTime(DateTime.Now.Year, EventMonth, EventDay);
    }

    public void Update()
    {
        for (int i = 0; i < EventsList.Length; i++)
        {
            EventsList[i].EventMonth = Mathf.Clamp(EventsList[i].EventMonth, 1, 12);
            EventsList[i].EventDay = Mathf.Clamp(EventsList[i].EventDay, 1, 31);

            //DateTime startEvent =

            //DateTime startChrismas = christmasTime.AddDays(-10);
            //DateTime endChrismas = christmasTime.AddDays(10);
        }
    }
}

I get:

(33,53): error CS0103: The name EventMonth' does not exist in the current context (33,65): error CS0103: The name EventDay’ does not exist in the current context
(33,74): error CS1502: The best overloaded method match for System.DateTime.DateTime(int, int, int)' has some invalid arguments (33,74): error CS1503: Argument #2’ cannot convert object' expression to type int’

Pretty much the same thing I received before.

Now that I can see the full code, ‘EventList’ is in a different class, so it doesn’t exist within EventSystem.

Is there any way to fix this? I serialize all my info like this in a new class so in each element of the array they are all together.

When you say EventList doesn’t exist, what exactly do you mean? EventList is declared on line 29, which is inside that class, also from that I SHOULD be able to access all the variables within the EventsArray class using EventList .EventMonth for example.

I have managed to get this compiling without any errors, but it still is not working correctly.
No matter that the dates are set at, the object defined is always disabled.

using UnityEngine;
using System.Collections;
using System;

[System.Serializable]
public class EventsArray : System.Object
{
    [Tooltip("This is the name of the event.")]
    public string EventName;
    [Tooltip("This is the gameobject(s) enabled on the event.")]
    public GameObject EventObjects;
    [Header("Event Date")]
    [Tooltip("The Month of the event.")]
    public int EventMonth;
    [Tooltip("The Day of the event.")]
    public int EventDay;
    [Tooltip("The amount of days before the Event Day to start the event.")]
    public int DaysBefore;
    [Tooltip("The amount of days after the Event Day to continue the event.")]
    public int DaysAfter;
}
[ExecuteInEditMode]
public class EventSystem : MonoBehaviour
{
    public DateTime EventTime;

    [Header("Events Manager")]

    public EventsArray[] EventsList;

    public void Start()
    {
        for (int i = 0; i < EventsList.Length; i++)
        {
            EventTime = new DateTime(DateTime.Now.Year, EventsList[i].EventMonth, EventsList[i].EventDay);

            DateTime startEvent = EventTime.AddDays(-EventsList[i].DaysBefore);
            DateTime endEvent = EventTime.AddDays(EventsList[i].DaysAfter);

            EventsList[i].EventObjects.SetActive(DateTime.Now.Ticks > startEvent.Ticks && DateTime.Now.Ticks < endEvent.Ticks);
        }
    }

    public void Update()
    {
        for (int i = 0; i < EventsList.Length; i++)
        {
            EventsList[i].EventMonth = Mathf.Clamp(EventsList[i].EventMonth, 1, 12);
            EventsList[i].EventDay = Mathf.Clamp(EventsList[i].EventDay, 1, 31);
        }
    }
}