- Home /
Deactivating a power up script after a certain amount of time
Hello I'm working on a simple power up script that allows the user to click on a button to slow down the movement of the player. 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's got me stumped is how to make the power up script only activate for a certain amount of time before it becomes deactivated and the player's movement goes back to normal.
Thank you for any answers or feedback. -Ben
Answer by Sundar · Oct 16, 2012 at 06:18 PM
You can use coroutine like
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;
}
Thank you very much for your answer. Exactly what I needed. For future reference I had to change the line 'function SlowDown( float deactivateInSeconds )' to 'function SlowDown(deactivateInSeconds : float)'.
Your answer
Follow this Question
Related Questions
Sprinting script, timer? 0 Answers
Need more buttons to this script. 1 Answer
Why my button in for loop isn't working? 1 Answer
Removing the tenths of seconds on a timer 3 Answers
How to use a global variable in javascript to set other variables data 0 Answers