Search Unity

Checking for the z position?

Discussion in 'Scripting' started by PalPenny, Mar 14, 2018.

  1. PalPenny

    PalPenny

    Joined:
    Aug 5, 2017
    Posts:
    11
    I'm designing a script that checks if a certain key is pressed, as well as if a certain GameObject is in the correct z position


    {
    public KeyCode qpress;
    public Transform qobj;
    public Transform perf1;

    void Update () {
    if (Input.GetKeyDown(qpress)) & (qobj.position.z <= perf1.position.z);
    }
    }


    However, visual studio gives me an error message on line 7, saying "Cannot take the address of the given expression".

    What am I doing wrong?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    You are using a single ampersand (&)... for logical "AND" operator you want to use double ampersand (&&).
     
    PalPenny likes this.
  3. PalPenny

    PalPenny

    Joined:
    Aug 5, 2017
    Posts:
    11
    I don't get the error anymore, but it does not output anything in game. Why is this


    void Update () {
    if (Input.GetKeyDown(qpress) && qobj.position.z <= perf1.position.z && qobj.position.z >= perf2.position.z)
    {
    Debug.Log("PERFECT");
    }
    }

    I don't get the error anymore, but it still does not output anything in game, even though the GameObject has a z coordinate between the two transforms. Why is this?
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Just put a breakpoint on the `if` and check the values. Maybe you have perf1 and perf2 swapped?

    Also, use
    Code (csharp):
    1.  tags to avoid the clipping you have in your last post.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187