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 /
  • Help Room /
avatar image
0
Question by Droki · Oct 17, 2016 at 12:54 PM · timer

Timer.deltaTime resetting once and only once instead of whenever I want.

As the title says, my timer float insists in only resetting once per game. What I'm trying to do here is create some logic for whenever the player gets a pickup. The pickup makes the player shoot faster. I'm fairly new to scripting and have looked everywhere for the answer to this. I can't figure it out. Help?

using UnityEngine; using System.Collections;

 public class PickUp : MonoBehaviour {
 
     float timer;
     public float powerUpDuration = 10f;
 
     GameObject pickup;
     ShooteyBall ShooteyBall;
     GameObject player;
     SphereCollider playerCollider;
 
     void Awake () {
         timer = 0f;
         player = GameObject.FindGameObjectWithTag ("Player");
         ShooteyBall = player.GetComponent<ShooteyBall> ();
         playerCollider = player.GetComponent<SphereCollider> ();
         ShooteyBall = player.GetComponent<ShooteyBall> ();
     }
 
     void ShootyTime(){
         timer = 0f;
         ShooteyBall.timeBetweenBullets = 0.05f;
     }
 
     void NoMoreShooty(){
         ShooteyBall.timeBetweenBullets = 0.15f;
     }
 
 
     void Update () {
         timer += Time.deltaTime;
         if (timer >= powerUpDuration) {
             NoMoreShooty ();
         }
     }
 
     void OnTriggerEnter (Collider other){
         if (other == playerCollider){ 
             ShootyTime ();
             Destroy(gameObject, 0f);
 
             }
     }
 
 }



Comment
Add comment · Show 2
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 doublemax · Oct 17, 2016 at 01:12 PM 0
Share

The gameobject with this script gets destroyed when the power-up is triggered, so it can only happen once.

I also noticed that the "pickup" variable is never used. $$anonymous$$aybe you want to destroy that one ins$$anonymous$$d of the gameobject in OnTriggerEnter ?

avatar image Droki doublemax · Oct 17, 2016 at 02:45 PM 0
Share

Tried doing as you suggested to no avail. I have been trying to figure this one out for quite a while and that reference to the pickup is probably debris from my desperate attempts to solve this :P

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by red-sight · Oct 17, 2016 at 01:33 PM

You are destroying the object in OnTriggerEnter, also you might find it easier if you do something like:

  1. Put the powerup timer in the ShooteyBall component

  2. In OnTriggerEnter tell the ShooteyBall component that a powerup has been collected, then destroy your power up

So there are two ways to do this, you can either use GetComponent or do a SendMessage to call a public method in that class eg (ApplyShootFasterPowerup). https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html

So in Pickup something like:

 public class PickUp : MonoBehaviour {     

      public float powerUpDuration = 10f;   
   
      void OnTriggerEnter (Collider other){
          if (other == playerCollider){ 
              other.gameObject.SendMessage("ApplyShootFasterPowerup", powerUpDuration); 
              Destroy(gameObject, 0f);     
              }
      }     
  }
 

Then in ShooteyBall something like:

     public bool powerUpApplied = false;
     public float powerUpDuration = 10f;
     public float powerUpTimer = 0f;   
     
      public void ApplyShootFasterPowerup (float duration) {
          powerUpApplied = true;
          powerUpTimer = 0f;
          powerUpDuration = duration;
          timeBetweenBullets = 0.05f;      
      }
 
     void cancelShootFasterPowerup(){
         timeBetweenBullets = 0.15f;
         powerUpApplied = false;
     }
  
      void Update () {
          if(powerUpApplied){
                  powerUpTimer += Time.deltaTime;
                  if (powerUpTimer >= powerUpDuration) {
                      cancelShootFasterPowerup();
                  }
          }          
      }
Comment
Add comment · Show 3 · 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 Droki · Oct 17, 2016 at 02:44 PM 0
Share

And all of a sudden everything works. I used your advice and it now does everything it's supposed to. Thanks! I don't understand why I was able to only change the timer once though. This game object is continuously instantiated on enemy kills. Why is destroying it when colliders touch disrupting the whole functionality though?

avatar image red-sight Droki · Oct 17, 2016 at 02:59 PM 0
Share

Each power up will be creating it's own instance of this class - what that means is that each power up will have it's own timer which will be getting destroyed :)

avatar image red-sight Droki · Oct 18, 2016 at 01:57 PM 0
Share

Would you $$anonymous$$d marking the question as answered? :)

avatar image
0

Answer by Droki · Oct 18, 2016 at 02:08 PM

Oh sorry. Thanks for the answer! I hadn't noticed the mail saying you answered me.

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

79 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

Related Questions

Adding a timer on my spawn script 1 Answer

How to start a timer after an update 0 Answers

Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer

Increment in every millisecond realtime? 1 Answer

How do I stop my timer by accessing a variable from another script? 2 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