When the game starts, the bomb drops from above with the spawn system. How to delete dropped bombs after 7 seconds
CodePudding user response:
There are many ways but easiest Destroy has an optional parameter
The optional amount of time to delay before destroying the object.
so just do e.g.
Destroy(enemyGameObject, 7f);
to destroy the GameOebjct after 7 seconds.
CodePudding user response:
You need to use a Timer (in "using System.Timers") in the bomb's script.
When the bomb spawns, the object's script Awake() method executes, and there you can setup and start your timer.
The problem is that you will need to create a new bomb object each time you spawn one, so i'm assuming your spawn system does that.
using System.Timers
Timer _timer;
private void Awake()
{
_timer = new Timer();
_timer.Interval = 7000; // in miliseconds
_timer.Elapsed = DestroyThisObject;
_timer.Start();
}
private void DestroyThisObject(object sender, ElapsedEventArgs args)
{
// Here you destroy your object. Be careful if the spawn system
// tries to access this object again as it could break the game.
}

