Destroying a precise instantiate object

So, here’s how it goes:
I got a script that will instantiate a prefab, chosen randomly from 8 prefabs. What has been instantiate can be destroyed by the player.
The thing is that i would like the object that has been instantiate to be automatically destroyed after a set amount of time.

Tell me if you want more precision.
Hope someone will be able to help me, thanks in advance.

Hey SpyRoudy

you could use a “CoRoutine” to delay a “Destroy” command, or the “Destroy” API offers a (time) variable.

public static void Destroy(Object obj, float t = 0.0F);
wher t = The optional amount of time to delay before destroying the object.

There are two ways.
utilizing the delayed Destroy API, which I have not actually had fantastic luck with.

and the method I tend to use as I can set it up inside of every 10th call or whatever.

make a variable called spawnTime as a type of double.
make another called lifetime or some such, also a double.
Inside your start or awake function set that variable to Time.time

then, inside update
use this (pseudocode)

Optionally Every 10th Update

IF

 Time.time - spawntime > lifetime

THEN

 Destroy.me

There are other options but none are well suited to unity.

Well thanks, with the help of both of you and a bit of playing aroud in the code i managed to find a way around my problem. Thanks!