- Home /
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);
}
$$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
Answer by hvilela · Oct 27, 2012 at 04:37 AM
The second parameter of destroy is the delay. You can use it.
I am trying to make the dead enemy body disappear after some time. But even without timer, the body doesnt disappear...
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);
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);
}
It gives this error: BCE0018: The name 'RigidBody' does not denote a valid type ('not found'). Did you mean 'UnityEngine.RigidbodyConstraints'?
it should be dead : Rigidbody = ..., just edited my answer
Your answer
Follow this Question
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