Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TysonG · Nov 26, 2018 at 10:22 PM · scripting problemcollider3dactivatetimed

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. :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GreenSerpent · Nov 26, 2018 at 11:28 PM

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TysonG · Nov 27, 2018 at 04:04 AM 0
Share

Thanks so much for replying, I appreciate it. So it is kind of working, except when I run the game I immediately have the power-up activated, it lasts for as long as I set the power up limit is set for, then it switches to the click-and-shoot script. I don't think my collider situation is right, cause when I shoot my power-up targets, nothing happens. I have an explosion prefab on the power-ups already, so when I hit it with a projectile it disappears. Would I have to add a second box collider and set it as a trigger or something? Extra info: - my two shooting scripts have variables that can be set, they are in my main camera (where the shots come from) -the scripts are the same, except that I had to change the getComponent collider part because I'm using a slightly older version of Unity I think. - The powerupCollision script is the script for the power-up cube.

2018-11-26-6.png (413.5 kB)
2018-11-26-8.png (421.2 kB)
avatar image TysonG · Nov 27, 2018 at 04:06 AM 0
Share

Other photos.

2018-11-26-7.png (425.3 kB)
2018-11-26-10.png (91.8 kB)
avatar image
0

Answer by TheGameLearner · Nov 27, 2018 at 04:27 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

201 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does anyone know how to activate different scripts OnTrigger? 1 Answer

How to switch between scripts on trigger? 1 Answer

My player's camera displays jerky objects 2 Answers

When I enable certain scripts on my player, the transform falls through the map. 0 Answers

Best way to make this grid 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges