- Home /
Destroy gameObject not working?
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(100);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
// Destroy ourselves
Debug.Log("Calling destroy for " + gameObject);
Destroy(gameObject);
}
Do you see any errors in your console log? Where are you initialising GOPos?
No, no errors. The debug for destroy doesnt pop up. Not sure what you mean about GOPos..
You have a reference defined to a GameObject called 'GOPos' and are using it to postion your replacement objects. I don't see where that is being set up.
What happens if you put an extra Debug.Log a the top of Replace? Is there any other way in your code of adjusting hitPoints, other than ApplyDamage?
Answer by bpears · Nov 04, 2012 at 06:32 PM
Thanks everyone. Problem was not my destroy script, it was that the dead body that replaces it did not have a destroy script. "DOH!" Now it works perfect. :)
Answer by deltamish · Nov 04, 2012 at 06:25 AM
what is the problem you face you need to be brief about your problem and what fix you want
Dont use speech marks to get componenet
GO.GetComponent(ScoreManager); correct
GO.GetComponent("ScoreManager"); wrong
O$$anonymous$$, this did help some. It at least got the debug to show up. But the dead enemy is still there.I want the dead enemy to vanish after some time.
Answer by soulburner · Nov 04, 2012 at 04:49 PM
GameObject.Destroy(go);
This does not make sense? Just causes error about unidentified (go)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Climbing script 0 Answers
My character will fall through the floor with this script:... 1 Answer
DamageScript 1 Answer
Help Solve This 2 Answers