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 bpears · Oct 27, 2012 at 04:26 AM ·

Destroy dead enemy object after time? script included

How can I implement destroying the enemy object after death, after a set time? Here is the script I have so far:

 ar maximumHitPoints = 100.0;
 var hitPoints = 100.0;
 var deadReplacement : Rigidbody;
 var GOPos : GameObject;
 private var scoreManager : ScoreManager;
 
 function Start(){
 
     var GO = gameObject.FindWithTag("ScoreManager");
     scoreManager = GO.GetComponent("ScoreManager");
 }
 
 function ApplyDamage (damage : float) {
     if (hitPoints <= 0.0)
         return;
 
     // Apply damage
     hitPoints -= damage;
     scoreManager.DrawCrosshair();
     // Are we dead?
     if (hitPoints <= 0.0)
         Replace(); 
 }
 
 function Replace() {
  
     // If we have a dead barrel then replace ourselves with it!
     if (deadReplacement) {
         var dead : Rigidbody = Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation);
         scoreManager.addScore(20);
         // For better effect we assign the same velocity to the exploded barrel
         dead.rigidbody.velocity = rigidbody.velocity;
         dead.angularVelocity = rigidbody.angularVelocity;
     }
     // Destroy ourselves
     Destroy(gameObject);
 }
 
 
 
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 whydoidoit · Oct 27, 2012 at 04:31 AM 1
Share

$$anonymous$$ight be worth checking out this UnityGems tutorial which covers destroying things after a period of time and having multiple step destruction and resurrection. Look for the timers section

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hvilela · Oct 27, 2012 at 04:37 AM

The second parameter of destroy is the delay. You can use it.

Comment
Add comment · Show 4 · 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 bpears · Oct 27, 2012 at 05:37 AM 0
Share

it doesnt disappear in this script either.

avatar image hvilela · Oct 27, 2012 at 05:39 AM 0
Share

Sorry? Could you explain what do you mean?

avatar image bpears · Oct 27, 2012 at 05:52 AM 0
Share

I am trying to make the dead enemy body disappear after some time. But even without timer, the body doesnt disappear...

avatar image hvilela · Oct 27, 2012 at 05:54 AM 0
Share

Have you added some Debug.Log to make sure everything is going well. For example:

 // Destroy ourselves
 Debug.Log("Calling destroy for " + gameObject);
 Destroy(gameObject);
avatar image
0

Answer by Sundar · Oct 28, 2012 at 04:33 PM

Try this

 var maximumHitPoints = 100.0;
 var hitPoints = 100.0;
 var deadReplacement : Rigidbody;
 var GOPos : GameObject;
 private var scoreManager : ScoreManager;
 
 
 function Start(){
 
     var GO = gameObject.FindWithTag("ScoreManager");
     scoreManager = GO.GetComponent("ScoreManager");
 }
 
 function ApplyDamage (damage : float) {
     if (hitPoints <= 0.0)
        return;
 
     // Apply damage
     hitPoints -= damage;
     scoreManager.DrawCrosshair();
     // Are we dead?
     if (hitPoints <= 0.0)
        Replace(); 
 }
 
 function Replace() {
 
     // If we have a dead barrel then replace ourselves with it!
     if (deadReplacement) {
        var dead : Rigidbody = Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation);
        scoreManager.addScore(20);
        // For better effect we assign the same velocity to the exploded barrel
         dead.name = "I am dead";
        dead.rigidbody.velocity = rigidbody.velocity;
        dead.angularVelocity = rigidbody.angularVelocity;
     }
     
      yield WaitForSeconds(2);
      Destroy( GameObject.Find( "I am dead" ) );
     // Destroy ourselves
     Destroy(gameObject);
 }
Comment
Add comment · Show 3 · 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 bpears · Nov 01, 2012 at 09:52 PM 0
Share

It gives this error: BCE0018: The name 'RigidBody' does not denote a valid type ('not found'). Did you mean 'UnityEngine.RigidbodyConstraints'?

avatar image Sundar · Nov 02, 2012 at 01:39 AM 0
Share

it should be dead : Rigidbody = ..., just edited my answer

avatar image bpears · Nov 04, 2012 at 02:52 AM 0
Share

When I try that, my enemies don't die...

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

11 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

How to change scripts to include Waves of Enemy? 0 Answers

Whats wrong here? 0 Answers

Script help 2 Answers

FPS Ai help 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