Light on and off problems

i have a script that turns a light on and off depending on the rotation of an object, but its not working correctly, can you see if you guys can help me fix it?

if (rotate.rotation.x >= 269  rotate.rotation.x <= 91) {
	sun.enabled = true;
}

else if (rotate.rotation.x < 269 || rotate.rotation.x > 91) {
	sun.enabled = false;
}

The first part of your if statement will never work, as it’s not possible for a value to be both >= 269 AND <= 91 at the same time.

ok, so i switch it, now what? (im sorry, i just cant think this through right now, my brain cant focus)

You probably want something like this:

if (rotate.rotation.x >= 91  rotate.rotation.x <= 269) 
{
   sun.enabled = true;
}
else
{
   sun.enabled = false;
}

Though that may be exactly backwards as your initial intent is difficult to determine. If so, just reverse the true/false settings.

um, its still not working, at all

I assumed you just needed help with the if-block logic itself, so that’s what I provided. Looking closer, the rotate.rotation.x looks suspicious…

What’s rotate? Transform.rotation stays within a magnitude of one, from zero. Hopefully rotate isn’t a Transform, but if it is, that won’t work. Transform.eulerAngles can be greater than 180, but it will get recalculated at some point.

I had some trouble testing this so I don’t have much help, but a question I have is, what object are you rotating? And are you certain that your are rotating it on the “x” axis. Because according to your script it looks like your asking for its “x” axis. And if your still having trouble, take a cube or something in a new scene, set up your “sun” and rotate your cube on its “x” axis. Its good to isolate your scripts if your having problems. So just making it an object and your sun. Rotate your object and see if its still not working. I made a very easy to use rotate script for you. It rotates whatever you put it on on its “x” axis.

var spinspeed = 0;
var text1 = "text";
var text2 = "text";

function Update () 
{
	transform.Rotate(spinspeed,0,0);
	
	if(spinspeed <= 0)
	{
		spinspeed = 0;
	}
	if(spinspeed >= 10)
	{
		spinspeed = 10;
	}
	
	if(spinspeed == 0)
	{
		text1 = "Roll Back";
		text2 = "Start Spinning";
	}
	if(spinspeed >=1)
	{
		text1 = "Spin Slower";
		
		text2 = "Spin Faster";
	}
	if(spinspeed == 10)
	{
		text2 = "Max Spin";
	}
}

function OnGUI ()
{
	GUI.Label(Rect(150, 10, 500, 500), "Max Spin Speed is ten. Current Speed: "+spinspeed);
	if(GUI.Button(Rect(10, 10, 100, 80), ""+text2))
	{
		spinspeed ++;
	}
	if(GUI.Button(Rect(10, 130, 100, 80), ""+text1))
	{
		spinspeed --;
	}
}
using UnityEngine;
using System.Collections;

public class _Time : MonoBehaviour {
	public Transform rotate;
	public float timeScale = 1.0f;
	private Light sun;
	
	void Start () {
		sun = rotate.GetComponentInChildren<Light>();
	}
	
	void Update () {
		rotate.Rotate((Time.deltaTime * timeScale) / 240, 0, 0);
		
		if (rotate.rotation.x >= 91  rotate.rotation.x <= 269) {
   			sun.enabled = true;
		}
		
		else {
			sun.enabled = false;
		}
	}
}

here is the whole script, if you find a solution, contact me via PM, because im going to be gone for a while, until next saturday

im pretty sure im not having any problems with rotation, just the light

rotate.rotation is a quaternion, not a set of Euler angles.

He’s right. If you want euler angles, use Transform.eulerAngles or Transform.localEulerAngles

it worked, the problem was it was going from 0y 0z to 180y 180z, and that screwed it up.