How to change between scripts on trigger?

I’m pretty new to C# so sorry if this is an easy script to figure out.

  • So I have a box collider on a cube, that when I shoot it with a projectile I want it to activate a power-up, which is another script. The original script is a click-and-shoot script, and the power-up script allows the player to click-and-hold to rapid fire. I know it has something to do with tags to trigger the script switch when a projectile hits it. I also wanted the power-up script to only last for a set amount of time before reverting back to the click-and-shoot script.

Any help would be much appreciated. :slight_smile:

You would want to add a tag called “Projectile” to your projectile. Then, you would want to add this in a script on the cube:

public PowerupManager powerupScript;
void OnTriggerEnter (Collider other) {
     if (other.collider.gameObject.tag == "Projectile") {
          powerupScript.powerupTime = 0;
     }
}

Then, make a script called “PowerupManager” with this in it:

public float powerupLength;
public float powerupTime;
public RapidFireScript rapidFire;
public SingleShotScript singleShot;
void Update () {
     powerupTime += Time.deltaTime;
     if (powerupTime > powerupLength) {
          powerupTime = powerupLength;
          rapidFire.enabled = false;
          singleShot.enabled = true;
     } else {
          rapidFire.enabled = true;
          singleShot.enabled = false;
     }
}

Make sure you change “RapidFireScript” and “SingleShotScript” to the actual names of your power-up scripts. Then, add that script to your player and set the variables in the inspector. Set ‘powerupScript’ on the cube to be the script you just added to the player. ‘powerupLength’ is how long the power-up will last in seconds. ‘rapidFire’ and ‘singleShot’ are your power-up scripts that you wanted to enable and disable. Though it is needed, changing ‘powerupTime’ in the inspector will do nothing.

Make sure your projectile has a collider and either your cube or your projectile has a rigidbody. If you don’t want it to move around, you can always set the rigidbody to kinematic. Hopefully this works!

In @GreenSerpent 's script, add a bool to check whether we want to activate the powerUp.
Update function should add ‘powerupTime’ with Time.deltaTime and activate deactivate other scripts only when the bool is true.
you can set ‘powerupTime’ to zero whenever the trigger is fired and then start from there.
Keep the bool as False at starting to avoid adding to time, and then if bool is false, activate single click script.