Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Turn a script on/off??

Discussion in 'Scripting' started by mosquito53, Jun 9, 2016.

  1. mosquito53

    mosquito53

    Joined:
    Jun 9, 2016
    Posts:
    6
    Hi, does anyone know if there is a way to turn a script on or off like you can turn a component on or off. In my game the player collects a power up, and i would like the main player controller script to shut off for 30 seconds and the power up script to be active for the 30 seconds, giving the player a full auto gun, or a 3 round burst... It would be like selecting the check box while not in play mode, but i need that in a script. Does anyone know how I could do this in c# script? Thank you
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. mosquito53

    mosquito53

    Joined:
    Jun 9, 2016
    Posts:
    6
    So would I make a new script with that Enable Component script? Or would I add the script to an existing script I have already?
     
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,511
    You can if you want, but that sounds like overkill.

    You would have to enable/disable a script from another script component.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    You could do that with a script enable/disable, but if you're just swapping weapons, probably a better way to do it where your one script just changes what it's doing. Say a pistol does x damage and shoots at x speed, but then you get a machine gun that does more damage and shoots faster. You could modify the values or just change what happens when you hit your shoot button.

    Other option is you have a weapon script attached to a weapon and depending on what weapon you pick up depends on how it responds.