A
A
Andrey2020-09-20 14:38:35
Unity
Andrey, 2020-09-20 14:38:35

How to delete an object after the timer expires?

Good afternoon.
Objects are randomly created on the map (copies of the prefab).
They are looking for food.
If they don't find it after 5 seconds, they die.
If found, the countdown starts again.
Etc.

How I imagine it:

...
eaten = 0;
if (Прошло 5 сек и не нашли еду)
    Destroy(GameObject); // Удаляем объект с карты
    predator.Remove(gameObject); // Удаляем его из списка
else
    eaten++;
    ...
...


The problem is that somehow you need to bind your own separate timer for each copy of the object.
Maybe somehow you can add a new "variable / property / field" for gameObject, with the name "timer", and what would then be called: gameObject.timer = 5.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question