speed increase only when button is pressed(touch).

I want to increase object’s speed only when the button is pressed(touched), it should come back to normal speed when the button is not pressed(touched).

transform.Translate(direction * Time.deltaTime * speed);

I tried

public void SpeedUp(){
  speed * 10;
}

and added this to button. Speed is increasing when button is pressed but not coming back to normal when button is left.

  1. remove the on click and add event trigger component to the button
  2. in the event trigger add new event type and select PointerDown
  3. again add new event type and select PointerUp
  4. now you know the rest each event needs a public function one for pressing & other for releasing the same way you handle on click event.

if(Input.GetKey(KeyCode.Space))
{
speed = 10;
} else
{
speed = 1;
}