Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why am I getting these errors?

Discussion in 'Scripting' started by Treasureman, Apr 2, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I'm making a script for a few item pickup things (this isn't that script, this just helps with some things in drawers and stuff. Not important for the question.), and I got these errors...

    Assets/ClosetColliderOn.js(6,38): BCE0044: expecting ), found '='.
    Assets/ClosetColliderOn.js(6,40): BCE0043: Unexpected token: 135.

    Here's the script. What am I doing wrong?
    Code (JavaScript):
    1. var door : GameObject;
    2. var itemCollider : BoxCollider;
    3.  
    4. function Update () {
    5.  
    6.     if(door.transform.rotation.y = 135){
    7.         itemCollider.enabled = true;
    8.     }
    9. }
     
  2. jmora256

    jmora256

    Joined:
    Jun 1, 2014
    Posts:
    3
    1. Code (JavaScript):
      1. if(door.transform.rotation.y == 135){
      2.         itemCollider.enabled = true;
      3.     }
      missing "="
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Aside from that, Transform.rotation.y will never be 135; Transform.rotation is a quaternion. Even if it wasn't, never do float comparisons like that, use a range.

    --Eric
     
  4. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Okay, for some reason, it doesn't work. Here's the code. If it gets to 135, the box collider doesn't turn on...
    Code (JavaScript):
    1. var door : GameObject;
    2. var itemCollider : BoxCollider;
    3.  
    4. function Start () {
    5. itemCollider.enabled = false;
    6. }
    7.  
    8. function Update () {
    9.  
    10.     if(door.transform.rotation.y == 135){
    11.         itemCollider.enabled = true;
    12.     }
    13. }
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Look at @Eric5h5's answer above.
     
  6. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I saw, but I'm not sure how to do that, or entirely what it means.
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you want to access the euler angles, it's

    Code (CSharp):
    1. transform.rotation.eulerAngles
    Float comparison needs to be done with a range/threshold, just like he mentioned.
    You can either use Math.Approximately or roll your own comparison, for example with a greater range. It depends what you need.
     
  8. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Maybe something like this?
    Code (CSharp):
    1. if (Mathf.Abs(door.transform.rotation.eulerAngles.y - 135) < 2)
    This would check if the angle is between 133 to 137.