How to find the x value of a tagged object in 2d mode

I’m a teenage developer and I have never really coded in my life so I’m relying on video tutorials on how to use unity. What I want to be able to do is when I click a button the object move to the left or right of the screen. My solution to this is to create an if statement so that if the integer '‘xvalue’ is more than 0 then the object moves to the left else it moves to the right. This is my script so far, feel free to tell me if I’m doing anything wrong:

using UnityEngine;
using System.Collections;

public class lineswich : MonoBehaviour
{
    int linepos =  
   
    // Update is called once per frame
    void Update () {
   

        if(Input.GetKey(KeyCode.Space))
            GetNewLocation();
   
    }


    void GetNewLocation ()
    {
        // if line pos is more than zere move 5 units to left
        if( linepos > 0)
        {
            // ... do this.
       
        }

        // If it is neither of those then...
        else
        {
            // ... do this.

        }
    }
}

The voids are underlined with a red zigzag and when I hover over it is says “parser error unexpected symbol”.
Thanks for the help you give me.

So I edited my code a bit and her it is now:

using UnityEngine;
using System.Collections;

public class lineswich : MonoBehaviour
{
    public int x = 2.5; 


    // Update is called once per frame
    void FixedUpdate () {
        if(Input.GetKey(KeyCode.Space))
            GetLocation();
   
    }


    void GetLocation () {
        // if line pos is more than zere move 5 units to left
        if(x > 0)
        {
            // ... do this once.
            transform.Translate(new Vector3(-5,0));
               
        }

        if (x = 0)
        {
            transform.Translate(new Vector3(2.5,0))
       
        }
            // If it is neither of those then...

        else
        {
            // ... do this once.
            transform.Translate(new Vector3(5,0));

        }
    }
}

This is seriously the first code I have ever written but I realized that I dont need to know the new x value by finding the location ingame but I can just do something like this

else
        {
            // ... do this once.
            transform.Translate(new Vector3(5,0));
            x+5

So in that example when the object moves forward 5, 5 is added to the current value of x.

I also have the problem of when I press the spacebar once it doesn’t just change positions once but multiple times.

I figured this out myself

Sharing is caring, so please share your solution :slight_smile: