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.
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.