Deactivating power up after certain period of time

Hello I’m working on a simple power up that allows the player to slow down. Here’s the script I’m using:

var mySkin : GUISkin;
function OnGUI () {

    GUI.skin = mySkin;    

    if (GUI.Button (Rect (10, 50, 80, 30), "Slow Down")) {
    var script4 = GetComponent("Movement"); 
    script4.enabled = false;
    var script5 = GetComponent("Movement - Easier"); 
    script5.enabled = true;
    }
}

What I’m having trouble with is keeping the power up only active for a certain period of time before it becomes deactivated and the player’s movement returns to normal.
I was kindly provided a coroutine script at another forum:

var mySkin : GUISkin;
function OnGUI () {

    GUI.skin = mySkin;    

    if (GUI.Button (Rect (10, 50, 80, 30), "Slow Down")) {
        StartCoroutine( SlowDown( 2.0 ) );// deactivate for 2 secs
    }
}

function SlowDown( float deactivateInSeconds )
{
    var script4 = GetComponent("Movement"); 
    script4.enabled = false;
    var script5 = GetComponent("Movement - Easier"); 
    script5.enabled = true;

    yield WaitForSeconds( deactivateInSeconds );

    script5.enabled = false;
    script4.enabled = true;

}

But that’s as far as I was able to get. I realise that ‘deactivateInSeconds’ is an Unexpected token so I removed it and tried to play around with the coding, with limited knowledge, to no avail. I’m not sure what to do.
Any answers or feedback would be greatly appreciated. -Ben

Right here:

function SlowDown( float deactivateInSeconds )

You’re declaring the variable as we do in C#, despite you’re using UnityScript. The correct declaration would be:

function SlowDown(deactivateInSeconds : float)

A quick tip: if your scripts aren’t of the same type, don’t name them script1, 2, 3, whatever. For organization, you should use descriptive names. :wink:

Thank you very much, worked like a charm. I’ll keep that tip in mind :slight_smile: