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. $$anonymous$$aybe you want to destroy that one ins$$anonymous$$d of the gameobject in OnTriggerEnter ?
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
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:
Put the powerup timer in the ShooteyBall component
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();
}
}
}
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?
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 :)
Would you $$anonymous$$d marking the question as answered? :)
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094815im_/https://answers.unity.com/themes/thub/images/avi.jpg)