Timer only goes for .02 seconds
Hi, I'm trying to make a timer that starts when a certain gameObject hits another gameObject. I have that part figured out, but I can't figure out why it only go for the split second (.02 seconds to be exact) that the gameObjects collide with each other. I want it so that let's say the timer is 15 seconds, so that it starts when the gameObjects collide and then continue even after one of the gameObjects are destroyed (because thats what happens when they collide). Any ideas? Let me know if anything isn't clear, Thanks!
The fact that it's .02 exactly is telling... that is the default time step in the time settings I believe. Sounds like you are only ever executing 1 frame (or maybe this is just called on start and not update?)
You are going to have to post your code for help, but my guess is that you aren't actually working in the update loop? Or you have some other code to change/stop the time/timescale.
Answer by acorrow · Dec 14, 2015 at 03:14 AM
You want the countdown on the OTHER object that it collides with, not this one. Move all your timer code and what not over to a script on your other object, and create a PUBLIC void to trigger from your Powerup.
So in the power up script, on your Collision, just grab the script off the colliding object, and call a method in it...
void OnTriggerEnter2D(Collider2D collider)
{
OtherObjectsScript otherThing = collider.GetComponent<OtherObjectsScript>();
otherThing.MyCountdownStart();
}
Sorry, I'm not really getting what to do. I thought I did what you told me to do, and it's still only going for .02 seconds. $$anonymous$$aybe it's because I'm a beginner, maybe since its late at night here. I'll check it again tomorrow. Thanks for everything so far
So yeah... I don't really know what to do. I've spent like a week on this. I think I'll just give up and maybe come back to it later. Thank you for all the help.
It's really very simple. You are just thinking about it wrong:
You have 2 objects, a Player, and a Power Up. When the player collides with the power up, the power up needs to be destroyed, and the player needs to be effected someway, for a specific time period.
What you need, is 2 different game objects (Player and PickUp) each with a different Script (call them Player.cs and PickUp.cs). Give the player object a tag of "Player" and the pickup object a tag of "Pickup" (I prefer to use layers here, but this is basic on purpose).
You place your TI$$anonymous$$ER in Player.cs, and control it with a bool. Like this: Player.cs
public class Player : $$anonymous$$onoBehaviour {
public bool Hit { get; set; }
public float timeRemaining = 15;
public int Armor { get; set; }
void Update()
{
if (Hit)
{
timeRemaining -= Time.deltaTime;
//GUI Stuff
if (timeRemaining <= 0)
{
Hit = false;
timeRemaining = 15;
}
}
}
}
Pickup.cs
public class Pickup : $$anonymous$$onoBehaviour
{
OnTriggerEnter2D(Collider2D collider)
{
if (collider.CompareTag("Player"))
{
//Now that we know it was the player we collided with,
//get the script attached to the Player game object and do stuff with it
Player player = collider.GetComponent<Player>();
player.Hit = true; //Setting Hit (bool) to TRUE will cause the time to decrease in the update loop.
player.Armor += 20; //Then you can access other parts of the Player script
}
}
}
One object can use GetComponent (assu$$anonymous$$g the script you wrote is a monobehavior) to grab a script from another and interact with it.
Try this approach.
Your answer
Follow this Question
Related Questions
Perform action If Input is done in this time. 2 Answers
How do I stop my timer by accessing a variable from another script? 2 Answers
Editable Timer 0 Answers
Start and Stop Timer Help 1 Answer