- Home /
Once game is built, it crashes when enemy dies.
Hey guys, so I got this problem; when I build and run my game and kill an enemy my game just closes like it crashed. This happens everytime with every enemy. When i play the exact same game in Unity it doesn't crash when I kill an enemy. What could cause this? Anything I should look into? Do you need to see any code?
UPDATE: It has to do something with these two codes, because I have both of them only on crates, and when I try to destroy the crates my game crashes. So that's good I guess...
UPDATES AGAIN: I'm pretty sure it has to do with the health code, I took off the destroy object code and it still crashed.
Code attached to enemy: Default Untiy3d Health Code
#pragma strict
public var maxHealth : float = 100.0;
public var health : float = 100.0;
public var regenerateSpeed : float = 0.0;
public var invincible : boolean = false;
public var dead : boolean = false;
public var damagePrefab : GameObject;
public var damageEffectTransform : Transform;
public var damageEffectMultiplier : float = 1.0;
public var damageEffectCentered : boolean = true;
public var scorchMarkPrefab : GameObject = null;
private var scorchMark : GameObject = null;
public var damageSignals : SignalSender;
public var dieSignals : SignalSender;
private var lastDamageTime : float = 0;
private var damageEffect : ParticleEmitter;
private var damageEffectCenterYOffset : float;
private var colliderRadiusHeuristic : float = 1.0;
function Awake () {
enabled = false;
if (damagePrefab) {
if (damageEffectTransform == null)
damageEffectTransform = transform;
var effect : GameObject = Spawner.Spawn (damagePrefab, Vector3.zero, Quaternion.identity);
effect.transform.parent = damageEffectTransform;
effect.transform.localPosition = Vector3.zero;
damageEffect = effect.particleEmitter;
var tempSize : Vector2 = Vector2(collider.bounds.extents.x,collider.bounds.extents.z);
colliderRadiusHeuristic = tempSize.magnitude * 0.5;
damageEffectCenterYOffset = collider.bounds.extents.y;
}
if (scorchMarkPrefab) {
scorchMark = GameObject.Instantiate(scorchMarkPrefab, Vector3.zero, Quaternion.identity);
scorchMark.active = false;
}
}
function OnDamage (amount : float, fromDirection : Vector3) {
// Take no damage if invincible, dead, or if the damage is zero
if(invincible)
return;
if (dead)
return;
if (amount <= 0)
return;
// Decrease health by damage and send damage signals
// @HACK: this hack will be removed for the final game
// but makes playing and showing certain areas in the
// game a lot easier
/*
#if !UNITY_IPHONE && !UNITY_ANDROID
if(gameObject.tag != "Player")
amount *= 10.0;
#endif
*/
health -= amount;
damageSignals.SendSignals (this);
lastDamageTime = Time.time;
// Enable so the Update function will be called
// if regeneration is enabled
if (regenerateSpeed > 0)
enabled = true;
// Show damage effect if there is one
if (damageEffect) {
damageEffect.transform.rotation = Quaternion.LookRotation (fromDirection, Vector3.up);
if(!damageEffectCentered) {
var dir : Vector3 = fromDirection;
dir.y = 0.0;
damageEffect.transform.position = (transform.position + Vector3.up * damageEffectCenterYOffset) + colliderRadiusHeuristic * dir;
}
// @NOTE: due to popular demand (ethan, storm) we decided
// to make the amount damage independent ...
// var particleAmount = Random.Range (damageEffect.minEmission, damageEffect.maxEmission + 1);
// particleAmount = particleAmount * amount * damageEffectMultiplier;
damageEffect.Emit();// (particleAmount);
}
// Die if no health left
if (health <= 0) {
health = 0;
dead = true;
dieSignals.SendSignals (this);
enabled = false;
Application.Quit();
// scorch marks
if (scorchMark) {
scorchMark.active = true;
// @NOTE: maybe we can justify a raycast here so we can place the mark
// on slopes with proper normal alignments
// @TODO: spawn a yield Sub() to handle placement, as we can
// spread calculations over several frames => cheap in total
var scorchPosition : Vector3 = collider.ClosestPointOnBounds (transform.position - Vector3.up * 100);
scorchMark.transform.position = scorchPosition + Vector3.up * 0.1;
scorchMark.transform.eulerAngles.y = Random.Range (0.0, 90.0);
}
}
}
function OnEnable () {
Regenerate ();
}
// Regenerate health
function Regenerate () {
if (regenerateSpeed > 0.0f) {
while (enabled) {
if (Time.time > lastDamageTime + 3) {
health += regenerateSpeed;
yield;
if (health >= maxHealth) {
health = maxHealth;
enabled = false;
}
}
yield WaitForSeconds (1.0f);
}
}
}
/*function OnTriggerEnter(other : Collider){
if(other.tag == "Health"){
health += 0.1;
Destroy(other.gameObject);
}
*/
***Destroy Object code
#pragma strict
var objectToDestroy : GameObject;
function OnSignal () {
Spawner.Destroy (objectToDestroy);
}
That's it, plus a movement code but I don't think that's necessary.
Need details brotha. Need to know what script you have attached to the enemy and the player and / or camera. It would be good to post any code attached to the enemies. That way we can see if there is something wrong with a script. You're obviously not getting any errors in the compiler, but are you getting any warnings at all in the console? Include any info like this and your issue should be resolved in a timely manner :)
When you run in the editor and kill something, do you get anything red in the error window? In editor mode, errors will skip some code and go on, so you may not notice. In the build, the same error will crash.
No I get no new red error codes when I kill the enemy. I get some when it spawns but it is just simple need to have a death explosion and stuff. Which I do add and it still crashes the game.
Accept the answer below by Jamora. Application.Quit() has no effect in the editor, but does in standalone application. It kills the process.
Answer by Jamora · Jul 01, 2013 at 12:11 AM
You have Application.Quit(); in line 98. Remove that and everything should work. It does nothing in the Editor, so that could be why your standalones seems to 'crash'.
OH $$anonymous$$Y GOSH! HOW COULD I HAVE $$anonymous$$ISSED THAT! Thank you so much for pointing it out and being kind that I missed something like that! Stay awesome my friend!
Your answer

Follow this Question
Related Questions
Why does my pathfinding script keep crashing Unity? 1 Answer
Distribute terrain in zones 3 Answers
Game Crash When Changing Scene 1 Answer
I need some health with my health/enemy script 0 Answers
Only one cloned enemy shoots 2 Answers