- Home /
Start a timer after instantiating a game object
I'm working on a variant of the Space Shooter game from Unity's tutorial. I have created a power-up object that instantiates a shield for your ship when touched, and I'm trying to implement a bar that indicates how long the shield will last. The bar itself is working fine, but I can't get the bar to start depleting the moment that the shield is instantiated.
My code for the GUI and shield looks like this:
private bool pup = false;
public float barProgress;
public Vector2 pos = new Vector2(10, 50);
public Vector2 size = new Vector2(20, 250);
public Texture2D emptyTex;
public Texture2D fullTex;
void OnGUI()
{
if (pup)
{
GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), fullTex);
GUI.BeginGroup(new Rect(0, 0, size.x, size.y * barProgress));
GUI.Box(new Rect(0, 0, size.x, size.y), emptyTex);
GUI.EndGroup();
GUI.EndGroup();
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "pup")
{
GameObject go = Instantiate(shield, shieldSpawn.position, shieldSpawn.rotation) as GameObject;
go.transform.parent = GameObject.Find("Power Ups").transform;
audio_shieldUp.Play();
audio_shieldDown.PlayDelayed(shieldDuration);
Destroy(GameObject.FindWithTag("Shield"), shieldDuration);
Destroy(other.gameObject);
pup = true;
}
}
void Update()
{
if (pup)
{
barProgress = Time.time / shieldDuration;
}
}
The shield instantiates and the bar shows up on screen, but the "barProgress" counter has already started counting and depletes too soon. What am I doing wrong here?
Also, any suggestions for how to remove the bar after shieldDuration has ended, as it's drawn in the script and not instantiated?
I suggest you strongly consider familiarizing yourself with Unity 4.6+'s UI system. The GUI calls you're using are considered legacy.
I believe your problem involves using Time.time as the numerator for this division. Time.time increments every frame and would therefore produce the results you describe.
Time-sensitive operations are usually ideal candidates for Coroutines. You could start the routine when you collide with the powerup, create-or-enable your bargraph object, allow it to run for X seconds, affecting your game and the bargraph progress, then ter$$anonymous$$ate itself, destroying-or-disabling the graph. Simple and clean. :)
How, though? What I don't get is why the Time.time ignores its parenting if-statement and ins$$anonymous$$d immediately starts counting when I start the game, even if I declare that barProgress = 0 the frame before.
Ah, using deltaTime to add time values to the barProgress variable was the solution. Thanks for the heads up!
Answer by AlwaysSunny · Mar 12, 2015 at 04:38 PM
Time.time isn't what you think it is, apparently. It doesn't start accumulating when you ask for it; it is always accumulating from the moment gameplay begins. Time.deltaTime returns how much time has passed in the last frame, which can be used to drive timer logic. Look into how to code timers, and how to use Coroutines. Best,