- Home /
Destroying Projectile Prefabs after Time
I am developing a Bullet Hell shooter that uses Object Pooling to spawn bullet prefabs that destroy themselves after a set number of seconds (with a default value of 5 seconds). When each bullet is destroyed, another bullet should spawn to take it's place.
A previous attempt at working through this caused all the bullets to destroy simultaneously and not spawn any more.
The idea for this to work is to have a 'bornTime' property be set once each prefab spawns and for the bullet to destroy itself 5 seconds later. At the moment I am receiving build errors and am unsure how to fix this.
This is the code that I am currently using:
private float bornTime;
private float currentTime;
public float DateTime;
static void Main()
{
//DateTime value = new DateTime(); //Should get a new DateTime value to be set in the currentTime update.
}
void onAwake()
{
var life = 5f; //No. of seconds that the projectile should be present for.
(bornTime=Time); //Should set bornTime to the time the object spawns.
}
void onUpdate()
{
currentTime.DateTime.Now.To.String(); //Sets the current time to whatever time it is at the moment.
if ((currentTime - bornTime) > 5f)
{
null(this); //Should destroy the object 5 seconds after spawning.
}
}
To note, there are many flaws and oddities in your script in its current state:
1) DateTime is the name used by "System.DateTime". Reusing that name for a variable, while not necessarily wrong, is not a good idea if you want to prevent confusion.
2) "$$anonymous$$ain()", "onAwake()", and "onUpdate()" are not automatically called by Unity during a game's logic-processing cycles. Numerous functions ARE called, however, including "Start()", "Awake()", and "Update()".
3) In "onAwake()", you define...
var life = 5f;
... then promptly ignore it; in "onUpdate()", you include the line...
if ((currentTime - bornTime) > 5f)
... which suggests your intent was to base it on "life" -- The "life" variable should ins$$anonymous$$d be defined outside the functions so it is not lost outside of its local scope.
4) In "onUpdate()", the line...
currentTime.DateTime.Now.To.String();
... cannot do anything. currentTime is your float variable, and is not related to "System.DateTime" in any way. System.DateTime, however, does include the rest of the functionality described, but in its current state, the best it could do would be to get the current date and time, convert it to a string, then do nothing with it and move on to the next line.
I just want to inform people here that I have attempted to use the methods suggested by users here and none have worked out for me. @jdean300 's answer failed to change anythind and @Yoshinator2 's answer prevented any projectile prefabs from spawning at all.
If there are any further suggestions to fix this, then please inform me asap. Thank you for your support regardless.
Answer by jdean300 · May 18, 2017 at 07:03 AM
This seems much simpler - just add this script to the bullet prefabs:
public class Lifetime : MonoBehaviour
{
public float TimeToLive = 5f;
private void Start()
{
Destroy(gameObject, TimeToLive);
}
}
I have found the source of the problem, Turns out there were two scripts on the prefabs that were causing problems between each other.
I have amended this and implemented this script and now the prefabs spawn clones within the hierarchy.
Thank you for the assistance.
Answer by tMahon · May 22, 2017 at 05:00 PM
You could also use a Co-routine, essentially the same thing but doesn't hold up the thread in an update loop.
void OnAwake () {
StartCoroutine(DestroySelfAfterSeconds(float destroyTime:5f));
}
Enumerator DestroySelfAfterSeconds(destroyTime){
return new WaitForSeconds(destroyTime);
Destroy(gameObject);
}
Answer by Yoshinator2 · May 18, 2017 at 12:56 AM
There is a much easier way to do this. Simple subtract time.deltatime from 5 and remove it when it is less than 0.
void Update () {
liveTime -= Time.deltaTime;
if (liveTime <= 0)
{
Destroy(this.gameObject);
}
}
If this solved your problem, please accept my answer! If not, feel free to ask questions.
Your answer
Follow this Question
Related Questions
Why are my bullets shooting at an unpredictable rate? 3 Answers
Instantiate bullet in time interval for "rifle". 2 Answers
Spawn a prefab at different objects 2 Answers
How do I make it so that my bullet projectile for my gun does damage? 1 Answer
How to script independent time tracking of prefabs? 1 Answer