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);

			}
	}

}

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. Maybe you want to destroy that one instead of the gameobject in OnTriggerEnter ?

2 Answers

2

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). Unity - Scripting API: GameObject.SendMessage

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();
                 }
         }          
     }

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?

Here you go. https://drive.google.com/open?id=0B0JgIhCTI6uja3NDLURXWHdTWWMxOXo2QUp0UkFiUEpDV1lF

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

hmm, that what I was thinking, try to tweak value so warning would disappear and rebake