How do you stop and restart a script to use for time limited powerups

Hi, I am wanting to implement a double score powerup which lasts X number of seconds when a button is clicked. I have managed to make a script to make this work, however I am unable to reactivate the script by clicking the same button for a second time. I think there is some fault in my disabling of the script which prevents me from reloading it. Also the debug log suggests the script does start initially with the game, and is run again when I click the button so am not sure if this has any affect.

I am fairly new to coding and unity and any help/advice would be appreciated.

I have made the following script

{
public bool DoubleClickSolar = false;
public GameObject DoubleScore;
public double Numberofsolar = 5f;

void Start()
{

}
void Update()
{
    Numberofsolar = BuySolarClick.SolarPerClick;
    if (DoubleClickSolar == false)
    {
        DoubleClickSolar = true;
        StartCoroutine(StartDoubleClickSolar());
    }
}
IEnumerator StartDoubleClickSolar()
{
    {

        Debug.Log("Starting POWERUP");
        DoubleScore.gameObject.SetActive(true);
        BuySolarClick.SolarPerClick *= 2;
        yield return new WaitForSeconds(10f);

        Debug.Log("END POWERUP");
        DoubleScore.gameObject.SetActive(false);
        BuySolarClick.SolarPerClick /= 2;
        GetComponent<PowerUpSolarClick>().enabled = false;

    }

}

}

The Update() method is called once every frame, DoubleClickSolar is initialized with false, hence the condition in the Update Method evaluates true in the first frame when you start the game.

I assume the issue is related to

a) how you set the DoubleClickSolar flag to true or false? Shouldn’t the condition check for DoubleClickSolar == true?

b) or GetComponent<PowerUpSolarClick>().enabled = false; Does PowerUpSolarClick need to be re-enabled maybe? Looks like you are enabling the DoubleScore GameObject and disabling the PowerUpSolarClick Component.


Alternatively, you could rename the Update Method, and assign it directly to the OnClick Handler of your Button. This way, you could get rid of the DoubleClickSolar flag.

Hi, Thanks for your reply.

I changed the false to true but then the script does not work.

When you say use an onclick button. Do you mean have a button which makes the script become true and the it becomes false after a certain time.

Sorry I am quite new to all this so not familiar with a lot of the jargon.