Need Help Controlling street light based on X axis rotation of Directional light.

Hello all,

I’ve setup a directional light to revolve around the X axis to make a day/night cycle and I want to control some street lights to come on when the “Sun” is down and turn off when the Sun comes up.

So I wrote two scripts to do this BUT obviously it doesn’t work. in the inspector when I play the directional rotation script the X axis increases until it reaches ~180 degrees then converts to ~179 degrees. In the inspector the sun rises a 0 on the X axis and sets at the ~180/-179 position

I figured I could just do something like if > 0 turn light off and if < 0 turn light on. this kinda worked. It would turn on for one complete rotation, then turn off for one complete rotation. I changed the code now it doesn’tt seem to work at all.

Could someone tell me what i am doing wrong please.

here is my code
sun rotation script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spin : MonoBehaviour {

    public float rate = 10.0f;
    private float x = -15.0f;

    void Update ()
    {

        x += Time.deltaTime * rate;
        transform.rotation = Quaternion.Euler(x, 0, 0);
 
    }
}

Light switch script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class switchOn : MonoBehaviour {

    public GameObject sun;
  
    private Light streetLight;

    void Start ()
    {
        streetLight = GetComponent<Light>();
        streetLight.enabled = false;
    }
  
    void Update ()
    {
        if(streetLight.enabled == true)// turn off
        {
            if (sun.transform.rotation.x > 0)
            {
                streetLight.enabled = false;
            }
        }

        if (streetLight.enabled == false)//turn on
        {
            if (sun.transform.rotation.x < 0)
            {
                streetLight.enabled = true;
            }
        }
      
    }
}

I found a work around, probably not the most elegant method but it works.

here is the code if anybody need a day/night cycle with working street lights

code for the sun:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spin : MonoBehaviour {

    private float rate = 6f; // speed of the rotation of the "Sun"
    private float x = 120; //starting postion of the Sun. 0 == Dawn, 180 == Dusk


    void Update ()
    {

        x += Time.deltaTime * rate;
        transform.rotation = Quaternion.Euler(x, 0, 0);

        if(x > 360)
        {
            x = 0;
        }
    }
}

code for the street lights:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class switchOn : MonoBehaviour {


    private float rate = 6f; // Needs to be set to the same value as rate in "Spin script"
    private float x = 120;  // Needs to be set to the same value as rate in "Spin script"

    private Light streetLight;
     

    void Start ()
    {
 
        streetLight = GetComponent<Light>();
        streetLight.enabled = false; // if starting game druing night time this bool need to be TRUE
    }
   
    void Update ()
    {

        x += Time.deltaTime * rate;

        if (x > 360)
        {
            x = 0;
        }

        if (streetLight.enabled == false)// turn on
        {
            if (x >= 170 && x < 360)
            {
                streetLight.enabled = true;
            }
        }

        if (streetLight.enabled == true)//turn off
        {
            if (x >= 4 && x < 180)
            {
                streetLight.enabled = false;
            }
        }  

     }
 }