If(variable.transform.rotation..etc not working?

Hiya.

I’ve been working on a slight puzzle as I’m slightly new with coding. I’m trying to make a puzzle game at the moment, but having the same rotation number works, BUT. I’m trying to make the script work with different numbers.

Take a look,

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

public class SewerPuzzle : MonoBehaviour
{
public GameObject Spotlight, Spotlight1, Spotlight2;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (Spotlight.transform.rotation.eulerAngles.z == 180 &
        Spotlight1.transform.rotation.eulerAngles.z == 180 &
         Spotlight2.transform.rotation.eulerAngles.z == 180)
    {

    
        Debug.Log("yes");
    }
}

}

As said, this works PERFECTLY. But after I change the numbers, differently. It doesn’t seem to work afterwards. Here’s an example.

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

public class SewerPuzzle : MonoBehaviour
{
public GameObject Spotlight, Spotlight1, Spotlight2;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (Spotlight.transform.rotation.eulerAngles.z == 120 &
        Spotlight1.transform.rotation.eulerAngles.z == 300 &
         Spotlight2.transform.rotation.eulerAngles.z == 20)
    {

    
        Debug.Log("yes");
    }
}

}

I’m still flustered on this, as I need the 3 to be completely different, and trigger something if all 3 are that rotations, I tried the difference of & (and) &&.

Thanks for the help if so!

Have you looked in the inspector to see what the actual z values are? I have seen those float values randomly go to 120.00054321 after assigning a hard coded 120. It may just be that it isn’t exactly the value you’re looking for.

The correct symbol you want to use is && (logical AND). Otherwise your code is correct. The problem you are having is actually likely a float issue. When you enter a value in the inspector, like “100” it doesn’t always get set to exactly 100 (sometimes it will come out as 100.0000005 or something like that). Try rounding the angles you are reading, like this.

if (Mathf.Round(Spotlight.transform.rotation.eulerAngles.z) == 120 &&
          Mathf.Round(Spotlight1.transform.rotation.eulerAngles.z) == 300 &&
           Mathf.Round(Spotlight2.transform.rotation.eulerAngles.z) == 20)
        {

            Debug.Log("yes");
        }