Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Tommas · Oct 20, 2011 at 07:51 PM · destroycomparetag

Kill object after time

So, I have a projectile prefab that is shot both by the player and by the turrets of my game. When I instance the prefab in the two different scripts, I give them two different tags, "wormProjectile" and "enemyProjectile". Because I didn't want to projectile to live forever, I attached a script to the prefab meant to kill it after a certain amount of time. I wanted the projectiles fired by the turrets to live a different length of time than the ones fired by the player, so I tried using the following script:

 var lifetimeP = 1.0;
 var lifetimeE = 2.0;
 
 function Awake ()
 {
     if (gameObject.CompareTag ("wormProjectile"))
     {
         Destroy (gameObject, lifetimeP);
     }
     if (gameObject.CompareTag ("enemyProjectile"))
     {
         Destroy (gameObject, lifetimeE);
     }
 }

But it never goes into the if statements. I am sure my mistake is something simple, but can anyone help me? Thank you! I really appreciate it.

Comment
Add comment · Show 1
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 timsk · Oct 20, 2011 at 08:00 PM 0
Share

well, your running this in Awake(). Awake gets called once, when the script is loaded (or "woken up" as i like to think of it). You need it to run every time a shot is fired (inside your shoot function?).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Oct 20, 2011 at 08:30 PM

I suspect Awake is called during Instantiate, and your problem seems to confirm that: since you set the tag after Instantiate, Awake only sees the original prefab tag.
A direct solution would be to change the routine from Awake to Start. A better solution, however, would be to fire the projectile and call Destroy in the same code - each shooting code would set the appropriate life time right after the projectile birth.
Your worm shooting script, for instance, would become something like this:

    ...
    bullet = Instantiate(prefab, ...);
    Destroy(bullet, 1); // if bullet isn't a GameObject, use bullet.gameObject
    bullet.tag = "wormProjectile";
    ...
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 Tommas · Oct 20, 2011 at 09:03 PM 0
Share

Thank you both! That worked great. I will get the hang of this eventually.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I make the Player be destroyed when I touch the end of the camera? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy object 1 if player walks into object 2 1 Answer

Destroy instance clone of gameobject 2 Answers

Problem with Score when enemy destroyed 0 Answers


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