Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question I need help to fix this error.

Discussion in 'Scripting' started by AnibalMB, Jun 15, 2020.

  1. AnibalMB

    AnibalMB

    Joined:
    Jun 14, 2020
    Posts:
    2
    Hi. Why is this error happening? "error CS0120: An object reference is required for the non-static field, method, or property 'Touch.position'"
    Code (CSharp):
    1. void Update()
    2.     {
    3.         Rect UpLeft = new Rect(-2f,-6.5f,-0.75f,-7.75f);
    4.         Rect Up = new Rect(-0.75f,-6.5f,0.75f,-7.75f);
    5.         Rect UpRight = new Rect(0.75f,-6.5f,-2f,-7.75f);
    6.         Rect Left = new Rect(-2f,-7.75f,-0.75f,-9.25f);
    7.         Rect Right = new Rect(0.75f,-7.75f,-2f,-9.25f);
    8.         Rect DownLeft = new Rect(-2,-9.25f,-0.75f,-10.5f);
    9.         Rect Down = new Rect(-0.75f,-9.25f,0.75f,-10.5f);
    10.         Rect DownRight = new Rect(0.75f,-9.25f,-2f,-10.5f);
    11.        
    12.         if (UpLeft.Contains(Touch.position))
    13.         {
    14.             gameObject.transform.position = new Vector2(-1.25f,-7.25f);
    15.         }
    16.         if (Up.Contains(Touch.position))
    17.         {
    18.             gameObject.transform.position = new Vector2(0f,7.25f);
    19.         }
    20.         if (UpRight.Contains(Touch.position))
    21.         {
    22.             gameObject.transform.position = new Vector2(1.25f,-7.25f);
    23.         }
    24.         if (Left.Contains(Touch.position))
    25.         {
    26.             gameObject.transform.position = new Vector2(-1.25f,-8.50f);
    27.         }
    28.         if (Right.Contains(Touch.position))
    29.         {
    30.             gameObject.transform.position = new Vector2(1.25f,-8.50f);
    31.         }
    32.         if (DownLeft.Contains(Touch.position))
    33.         {
    34.             gameObject.transform.position = new Vector2(-1.25f,-9.75f);
    35.         }
    36.         if (Down.Contains(Touch.position))
    37.         {
    38.             gameObject.transform.position = new Vector2(0,-9.75f);
    39.         }
    40.         if (DownRight.Contains(Touch.position))
    41.         {
    42.             gameObject.transform.position = new Vector2(1.25f,-9.75f);
    43.         }
    44.  
    45.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960