2D sample character controller asset : Script for powerup object that slows player

Hi guys,

I’m kind of new to unity and c# in general. I’m creating an infinite runner as my first project and so far I have a powerup that adds points to the players score but I’m also looking for a power"down" that slows the players maxspeed by ‘x’.

I’n wondering how can I access the variable maxSpeed when it is in another class? I know that each class is considered a component and you need two components on the same object to interact with each other… is that correct? The problem is that I obviously can’t put the player controller script onto the powerup?

Help seriously appreciated!

Are you adding the power up script to you player when they pick it up? If so you could do something like this:

//in my power up script
float speedReduction = 5.0f; // whatever or however you do speed
float effectDuration = 10.0f; // how ever long it lasts in seconds

PlayerControllerScript thePlayer;

void Start(){
     thePlayer = GetComponent<PlayerControllerScript>();

     thePlayer.maxSpeed -=speedReduction;

     Invoke("AllDone", effectDuration);
}

void AllDone(){
     thePlayer.maxSpeed +=speedReduction;
     Destroy(this); // <- pretty sure that's how you kill a component but may be wrong on this
}