Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NiceHat96 · May 18, 2017 at 12:37 AM · prefabtimebulletsbullet-prefab

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.
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eno-Khaon · May 18, 2017 at 03:49 AM 0
Share

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.

avatar image NiceHat96 · May 21, 2017 at 03:12 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NiceHat96 · May 22, 2017 at 03:49 PM 0
Share

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.

avatar image
1

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges