I want to make a tap to "teleport" code.. is this correct?

hye guys, I want to make a 2D racing game that has only 2 lanes. and I want to make the car(player) change its lane every single tap. From right lane, tap! then left lane. From left lane, tap! then right lane.

so this is my code. but compiler says parser error. expecting a close curly braces “{}” at the bottom… help me guys! :smiley:

void Update () 
	{
	 bool Left = true ;
		
	 Touch tap = Input.GetTouch (0);
	 if (Left == true) 
	{
	 if (tap.phase == TouchPhase.Ended && tap.tapCount == 1) 
	 {
	 transform.Translate (Vector2.right * 5 * Time.deltaTime);
	 Left = false;
	 }
	}
	else if (Left == false)
	{
	if (tap.phase == TouchPhase.Ended && tap.tapCount == 1)
	 {
	 transform.Translate (Vector2.right * -5 * Time.deltaTime);
	 Left = true;
	 }
	}
}

A few comments :

“Left” will always be true as it’s declared within the Update method and set to true every frame.

You could do :

    private bool left = true;
	void Update ()
	{
		Touch tap = Input.GetTouch ( 0 );

		if ( tap.phase == TouchPhase.Ended && tap.tapCount == 1 )
		{
			if ( left )
			{
					transform.Translate ( Vector2.right * 5 * Time.deltaTime );
			}
			else
			{
				transform.Translate ( Vector2.right * -5 * Time.deltaTime );
			}
			left = !left;
		}
	}

or even something like this :

private bool _left = true;
	public bool Left
	{
		get { return _left ; }
		set
		{
			if (value != _left)
			{
				_left = value ;
			}
		}
	}

	public float Offset
	{
		get
		{
			float offset = Left ? 5 : -5 ;
			Left = !Left ;
			return offset ;
		}
	}
	
	void Update ()
	{
		Touch tap = Input.GetTouch ( 0 );

		if ( tap.phase == TouchPhase.Ended && tap.tapCount == 1 )
		{
			transform.Translate ( Vector2.right * Offset * Time.deltaTime );
		}
	}