- Home /
Unity freezes when OnTriggerEnter occurs
I have been attempting to write a collection of scripts that deals with player health and an enemy object hitting a player object. This script is placed in a hitbox child of the enemy object and refers to a player health script which seems to be working correctly. When I approach the enemy however, Unity freezes up completely. I know that it is most likely some sort of loop or condition that is executing infinitely with little to no delay but I cannot seem to pinpoint exactly what it is. Most of the pieces of the script are modeled off of snippets of code that I have used before that worked so I am not quite sure exactly what the issue is. Any help would be appreciated. Here is the script.
#pragma strict
public var damageDone: float = 45f;
public var hitCoolDownTime: float = 1f;
public var meshObject: GameObject;
private var playerObject: GameObject;
private var playerHitBox: GameObject;
private var playerState: PlayerState;
private var hitCoolDown: boolean = false;
private var hitTimer: float = 0f;
//Crashes the game
function Awake () {
playerObject = GameObject.FindGameObjectWithTag("Player");
playerState = playerObject.GetComponent(PlayerState);
}
function Update () {
if(hitCoolDown){
hitTimer += Time.deltaTime;
if(hitTimer >= hitCoolDownTime){
hitCoolDown = false;
hitTimer = 0f;
}
}
}
function OnTriggerEnter (other: Collider){
if(other.gameObject == playerObject && !hitCoolDown && meshObject.GetComponent(SkinnedMeshRenderer).enabled){
playerState.Damage(damageDone);
hitCoolDown = true;
}
}
Answer by ThomasMarsh · Feb 07, 2015 at 12:02 AM
I managed to fix my own issue by modifying the code inside the OnTriggerEnter so that the object that collides with the trigger is checked first, and then the cooldown and meshrenderer are checked in an if statement nested one level below.
function OnTriggerEnter (other: Collider){
if(other.gameObject == playerHitBox){
if(!hitCoolDown && meshObject.GetComponent(SkinnedMeshRenderer).enabled){
playerState.Damage(damageDone);
hitCoolDown = true;
}
}
}
I also all the trigger events occur in a separate layer because I have other colliders on both the enemy and the player than the ones I use here. Now everything works perfectly. Thank you all for all the help.
Answer by MakeCodeNow · Feb 04, 2015 at 01:03 AM
Either meshObject or playerState is null.
Neither are null I have not gotten any null error messages in console and the script also works as it should for a split second before it freezes up.
The game works up until the point when the OnTriggerEnter occurs and then it freezes up.
$$anonymous$$ust be an infiniteLoop in the playerState.Damage() function.
In general crash != hang. A crash ter$$anonymous$$ates the application while a hang keeps running, just stuck in one spot.
The playerState.Damage() function contains one line of code which is playerHealth -= damageDone;
Is playerHealth a simple field or a property? If it's a property, maybe the code inside it is the issue?
Answer by alok-kr-029 · Feb 06, 2015 at 05:09 AM
I think so every time the trigger is entered the hitCoolDown = true and meanwhile in update every frame it gets updated and try running your if(hitCoolDown){ hitTimer += Time.deltaTime; if(hitTimer >= hitCoolDownTime){ hitCoolDown = false; hitTimer = 0f; } part of script which make an infinite loop and hangs your gameplay
Your answer
Follow this Question
Related Questions
Audio Fading too fast 2 Answers
2D Jump AI Help 0 Answers
How do I reset a for loop variable??? 3 Answers
PC crash when open Monodevelop 0 Answers