Search Unity

Range for Transform Value of GameObject

Discussion in 'Scripting' started by VFranklin, Oct 20, 2019.

  1. VFranklin

    VFranklin

    Joined:
    Jul 26, 2017
    Posts:
    48
    Hi,

    I know there's some simple error in my code, but I'm unsure as to what. Why can't I use < for gameObject.transform.position?

    I'm getting I can't use < with type int and bool.

    I want to detect when the gameObject is within a certain x and y range.

    How can I go about doing this?

    Thank you.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Object_Range : MonoBehaviour
    6. {
    7.     public GameObject gameObject;
    8.  
    9.     void SolveLevel()
    10.     {
    11.         if((-21< gameObject.transform.position.x < -19 ) && (-17< gameObject.transform.position.y < -15.5))
    12.         {
    13.             // level will be solved
    14.  
    15.         }
    16.     }
    17. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You have to test the above and below conditions separately, such as:

    Code (csharp):
    1. if ((low > a) && (a < high)) ...
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You have to explicitly write out the whole logical expression for each comparison:
    Code (CSharp):
    1. float xPos = gameObject.transfom.position.x;
    2. float yPos = gameObject.transfom.position.y;
    3.  
    4. if((xPos > -21 && xPos < -29) && (yPos > -17 && yPos < -15.5)) {
    5.  
    6. }