- Home /
Question by
Jammingjack99 · Feb 07, 2021 at 08:18 PM ·
c#unity 2dtimer
how to add time to a countdown
I have a countdown timer and I want to add more time when a Object is collected here's everything works except for the add time help
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "food")
{
pnc.countDownTimer += addNumber;
pnc.countDownTimer = Mathf.Min(pnc.countDownTimer, 10);
}
}
}
,im trying to add time to my timer when a game object is collided with the timer works but I cant add help
void Start() {
currentTime = startingTime;
}
void Update()
{
currentTime -= 1 * Time.deltaTime;
CountdownText.text = currentTime.ToString ("0") ;
if(currentTime <= 0)
{
currentTime = 0;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "food")
{
pnc.countDownTimer += addNumber;
pnc.countDownTimer = Mathf.Min(pnc.countDownTimer, 10);
}
}
}
Comment
Best Answer
Answer by RodrigoAbreu · Feb 07, 2021 at 08:37 PM
@Jammingjack99, Aren't you adding that amount to a wrong variable? Shouldn't it be:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "food")
{
currentTimer += Mathf.Min(addNumber, 10);
}
}
PS.: Make sure your "addNumber" is not 0.
its says addNumber dose not exist in this situation do i need to make a variable?
If you want a dynamic value that might be changed during the execution yes, if all you need is a fixed amount to be added every time, then just a constant should be enough.
PS.: Remember to close your question once it's solved.