Need help with coroutines

I’m doing a beginner unity course. I’m trying to add a powerup that when collided with will increase the players shooting speed. I’m using a coroutine to apply a waitforseconds to return back to the normal shooting speed. I can’t seem to work out what I’m doing wrong. Any help is much appreciated

5853112--621808--Screenshot_5.PNG 5853112--621808--Screenshot_5.PNG

remove your while from the coroutine, since you print screened it I cant copy paste what it should look like

also remove from start()

player = getcomponent<player>

its setting your player to null

1 Like

Thank you, that’s removed the error. However the powerup seems to only increase my fire rate after I stop my game and won’t increase during the gameplay

can you show your player script

You are destroying the gameobject that the coroutine is running on in your Pickup method

[[quote=“brigas, post:4, topic: 791009, username:brigas”]
can you show your player script
[/quote]
This is the player script https://pastebin.com/62ptthr4

](https://pastebin.com/62ptthr4)

How could I remove the powerup without stopping the coroutine?

Run the coroutine on the player instead. Move it to the player class and call a method to run it from there.

public class Player : MonoBehaviour
{
    [SerializeField]
    private float _projectileFiringPeriod;

    public void AdjustFirePeriod(float newRate, float rateLentgh)
    {
        StartCoroutine(SetFirePeriod(newRate, rateLentgh));
    }

    private IEnumerator SetFirePeriod(float newRate, float rateLentgh)
    {
        _projectileFiringPeriod = newRate;
        yield return new WaitForSeconds(rateLentgh);
        _projectileFiringPeriod = 0.2f;
    }
}

public class PowerUp : MonoBehaviour
{
    [SerializeField]
    private Player _player;

    [SerializeField]
    private float _powerUpSpeed, _rateLength;

    private void Pickup()
    {
        _player.AdjustFirePeriod(_powerUpSpeed, _rateLength);
        Destroy(gameObject);
    }
}
1 Like

As a workaround you can also deactivate the collider and all the power up renderes on Pickup() and then destroy when the corutine ends. Anyways, take in mind how to manage many pickups coexisting at the same time. Should they restart the timer? Should remainingTime + newTime get added? And so on :stuck_out_tongue:

1 Like

This is now working :smile: But after I pickup a second powerup I get the error
"Coroutine couldn’t be started because the the game object ‘Player’ is inactive!
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Player:AdjustFirePeriod(Single, Single) (at Assets/Scripts/Player.cs:68)
Powerup:pickup(Collider2D) (at Assets/Scripts/Powerup.cs:28)
Powerup:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/Powerup.cs:23)
" What does it mean by inactive?
Edit
Just noticed it works fine when I add my instanced player gameobject to my powerup, but the problem is with adding my prefab player to the powerup.
Edit 2
I’m stupid and didn’t realise my instanced powerup in the scene was a different prefab to the one dropped by my enemies lol. Got there in the end! Thank you so much for the help :slight_smile: