Adding Power Ups using AddComponent()
When it comes to adding temporary Debuffs or Buffs, then AddComponent() is the way that I like to go. That’s because you simply create short scripts to temporarily affect variables in the main component. This way you can avoid cluttering your main player script with bools and other functions and all you need to do is give access to certain variables for buffs.
Here’s an example of a short speed buff for the player:
Since the Speed buff will be added directly to the player, we can just get the player component directly.
Now the confusing bit is probably this line here:
Wouldn’t that destroy the entire GameObject? The answer is no. The script will only destroy itself and nothing else and since the duration is set to 10 then the component will destroy itself after 10 seconds.
Here’s an example of the TripleShot powerup instead which lasts 5 seconds: