GUI button speed up?

Guys, I have this java script:

var speed = 14;
var SecondsInTurbo = 10;

function OnGUI () {
	var speedup = GUI.Button (new Rect (0,Screen.height - 50,100,50), "Speed up");
	if(speedup == true){
	Debug.Log("Speeed!");
	speed = 20;

	}
	}

And I want to make speed to have value 20 for 10 secs. The point is - I can’t use yield WaitForSeconds (10);, because console gives me this error: Script error (JavaMotors): OnGUI() can not be a coroutine.. So how can I deal with this situation? Can I somehow use speedup variable in Update function?

How about something like this:

#pragma strict

var normalSpeed = 14;
var turboSpeed = 20;
var secondsInTurbo = 10;
private var speed = 14;

function Start() {
	speed = normalSpeed;
}
 
function OnGUI () {
    if (GUI.Button (new Rect (0,Screen.height - 50,100,50), "Speed up")) {
	    Debug.Log("Speeed!");
	    speed = turboSpeed;
	    Invoke("RestoreNormal", secondsInTurbo);
    }
}

function RestoreNormal() {
	speed = normalSpeed;
}

While this fixes the problem you asked about, the next step will probably be locking out turboSpeed for some amount of time after it is used. In addition, it would probably be best if the button was locked out during turbo speed as well.

I’m not familiar with Javascript, translation from C# shouldn’t be a big deal in this case. I would use a timer and Time.time:

float turboTimer;

function Start()
{
    speed = normalSpeed;
    turboTimer = 0;
}

function OnGUI () {
    if (GUI.Button (new Rect (0,Screen.height - 50,100,50), "Speed up")) {
        if (turboTimer == 0) {
          turboTimer = Time.time;
          speed = turboSpeed;
          Debug.Log("Speeed!");
        }
    if (turboTimer < Time.time - secondsInTurbo) {
        turboTimer = 0;
        speed = normalSpeed
    }
}