- Home /
Extend time by picking up object problem
I am playing with Unity's Catch Game tutorial, and trying to add an object that would extend time when picked up. I added a timer like this in fixed update:
void FixedUpdate(){
if (playing) {
timeLeft -= Time.deltaTime;
if (timeLeft < 0) {
timeLeft = 0;
}
UpdateText ();
}
}
and then i did this:
void OnCollisionEnter2D(Collision2D collision){
if (collision.gameObject.tag == "timer") {
timeLeft += 10;
}
}
I also tried with void Update(), but id did not work. Thanks in advance.
Is your timer collider set to isTrigger = true by any chance, that would be the norm for pickups and such, in which case you would need to use OnTriggerEnter2D.
It was not trigger, I've tried to make it trigger and used the OnTriggerEnter2D but it didn't work.
Answer by Ibzy · Jun 23, 2015 at 03:23 PM
Main thing I see here is that you don't call UpdateText()
after adding the time (though this still should show after the next FixedUpdate). Are you sure you are checking for the tag exactly? Is there a capital T maybe?
Try putting a Debug.Log("Time Added")
after timeLeft += 10;
to see if the collision tag is "timer", and a Debug.Log(collision.gameObject.tag);
outside the if statement to check an actual collision happened.
Thanks for the quick reply. I posted only small portion of the code, the update text is in the code:
void UpdateText(){ timerText.text = "Timer:\n" + $$anonymous$$athf.RoundToInt (timeLeft); }
It wasn't the tag, I copy-pasted it, and when I tried to log this and nothing happened, not even the collision :S
Sounds like the collision isn't happening then. Do you have a collider on both the timer object and the character (I imagine you do on the character for other drops to work) and a RigidBody on at least one (probably character again)?
I have both collider and rigidbody (2d) on both objects. When I pick up other objects I get points, so this should work as well, but as you said there is something with collision.
Could you share a snip of the inspector on both objects with all components "expanded"? $$anonymous$$ight help us see where there might be an issue :)
That will be your issue then - the collision isn't happening because the gamecontroller isn't attached to the hat (as far as I can tell in the tutorial?) and thus has not collider. Simply attaching the script to the Hat should suffice, but I think the cleaner approach would be to add your check inside OnTriggerEnter2D() into the existing one in BowlController and then adjusting the timeLeft variable (most easily as a static variable)
Your answer
Follow this Question
Related Questions
Skipping over timer 1 Answer
Countdown Timer or WaitForSeconds? 2 Answers
Is the 2D gameplay tutorial by Graveck Interactive outdated? 1 Answer
Any 2D side scroller tutorials? 2 Answers
Question about 2D Gameplay Tutorial 0 Answers