Move Button as soon as its klicked to the bottom Screen

Hey guys,

so i have this task of moving a button to the bottom of the screen as soon as its clicked. i tired different solutions:

var button : Texture;
var x : int = 20;
var bottom : int = (Screen.height - 20);
var time : int = 2;
var startTime : int = Time.time;
var speed : int = 100;

function OnGUI() {
    if (GUI.Button (Rect(10,x,80,40), button))
        Debug.Log("Es geht los!");
    //  x += 150*Time.deltaTime;    <<-- This way the button never stops to move
        
}

function Update() {                        <<-- this way the button stops at the middle of the screen (?)

    if(Time.time > startTime + time) {
        x += Time.deltaTime*speed;
        if (x > bottom) {
            x = bottom;
        }
    }
}

but none of them really worked.

i need the button to go to the bottom of the screen, and then move to the top and back like 5 times.

thanks in advance!

I would set a boolean value on button click

if (GUI.Button (Rect(10,x,80,40), button))
{
  bool moveButton = true;
}

Then in your Update method

while(moveButton){
  // Increment button towards the bottom of the screen
  if (buttonAtDesiredPosition)
  {
    moveButton = false;
  }
}

There’s some sudo code to get you started. Get rid of the Time.Time and DeltaTime stuff, I don’t think you need it.