Moving left and right with one button in 2d Game

Hi, I am working on a runner game where you try to dodge obstacles. What I am trying to do is on a single key press it changes directions. So if it is going left then key press it goes left and vice versa. I made a code but the problem is that it won’t switch directions. If you could, please look over and tell what is wrong with the code.

using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Rotate()
{
    transform.Rotate(Vector3.back * -20);
}
void Update () {
    int speed = 1;
    int move = 1;
    if (move == 1)
    {
        transform.Translate(Vector3.right * Time.deltaTime * speed);
        
    }
                
    if (move == -1)
    {
        transform.Translate(Vector3.left * Time.deltaTime * speed);
        
    }

    if(Input.GetKey("space"))
    {
        move = move * -1;
    }
   
            
    
        

}

}

P.S.
Later I want to make it an app. So how do I set it up so that if you tap the screen(anywhere on the screen) the character will switch directions. Thanks!

You need 2 bools for this…

    private bool moveRight = true;
    private bool buttonDown = false;

    public void ButtonIsDown()
    {
        buttonDown = true;
    }

    public void ButtonIsUp()
    {
        buttonDown = false;
    }
    
    public void SwitchDirection()
    {
         moveRight = !moveRight;
    }
    
    void Update()
    {
        if (buttonDown && moveRight)
        {
            transform.Translate(Vector3.right * Time.deltaTime * speed);         
        } else if (buttonDown) {
            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
    }

Then add a UI Button, you can get rid of the image and text part or put a canvas group and set Alpha to 0 (either way it’ll make it transparent).

Add an event trigger to it and add 2 new event types, one for Pointer Down and one for Pointer Up.

In Pointer Down click + twice and drag the player with that script on it onto the two slots that appear. Then from the drop down select YourScriptName → ButtonIsDown in one and YourScriptName → SwitchDirection in the other.

In Pointer Up click + once and drag the play (with the script) onto the slot. From the drop down select YourScriptName → ButtonIsUp.

You can test this in the editor as the UI accepts either touch or mouse clicks. You’ll need your speed variable in there as well.