- Home /
Application.LoadLevel - Causes crash!
I have this script attached to the player object, and I have another object to send a raycast to call the function "ApplyDamage". If i press "g" the level resets perfectly... But if i loose all my hitpoints my computer crashes and comes up with the unity bug reporter message...
var hitPoints : int=100;
var bloodTexture75 : Texture2D;
var bloodTexture50 : Texture2D;
var bloodTexture25 : Texture2D;
var bloodTexture10 : Texture2D;
function Update (){
if(hitPoints <= 0){
Application.LoadLevel(Application.loadedLevel);
}
if(Input.GetKeyDown("g")){
Application.LoadLevel(Application.loadedLevel);
}
}
function ApplyDamage (damage : int){
hitPoints -= damage;
}
function OnGUI (){
if (hitPoints <= 75){
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), bloodTexture75, ScaleMode.StretchToFill);
}
if (hitPoints <= 50){
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), bloodTexture50, ScaleMode.StretchToFill);
}
if (hitPoints <= 25){
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), bloodTexture25, ScaleMode.StretchToFill);
}
if (hitPoints <= 10){
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), bloodTexture10, ScaleMode.StretchToFill);
}
}
Ok, so I should give a bit more information on the issue... $$anonymous$$y player is a rigidbody, I use rigidbody.velocity to move and addforce to jump. I have animations for jumping, landing, idling, walking and sprinting. I have created some zombie AI to attack me, which use raycast. If I try jumping, it crashes when I press "g", but not for idling, walking and sprinting.
Answer by ujebegu · Jul 24, 2012 at 12:57 PM
try this: function Update (){ if(hitPoints <= 0){ hitPoints = 100; Application.LoadLevel(Application.loadedLevel); } if(Input.GetKeyDown("g")){ Application.LoadLevel(Application.loadedLevel); } }
Thanks for your help, but unfortunately it doesn't work! - I guess I will need to think of another way to respawn.
Answer by BeHappy · Jul 24, 2012 at 09:53 AM
Try to reset the value of hitPoints to 100 while loading the game again,
function Update (){
if(hitPoints <= 0){
Application.LoadLevel(Application.loadedLevel);
hitPoints = 100;
}
if(Input.GetKeyDown("g")){
Application.LoadLevel(Application.loadedLevel);
hitPoints = 100;
}
}
So when hitPoints comes to 100, update function will execute the statement "Application.LoadLevel(Application.loadedLevel);" redundantly and cause Unity to crash.
This only makes sense if the object with this script attached is persistent between levels. Otherwise the value are initialized again as it is a new object.
But to make sure that LoadLevel is not called more than twice, it is even better to set the value to 100 before calling the method. Hence:
hitPoints = 100;
Application.LoadLevel(Application.loadedLevel);
Another way (only if the object is not persistent between levels) is to destroy the component so that Update is not called again. But normally LoadLevel is synchrone (as opposed to LoadLevelAsync) so this issue should not arise.
Destroy(this);
Application.LoadLevel(Application.loadedLevel);
Even if I only call it for one frame, it will still crash ($$anonymous$$y $$anonymous$$d is boggled)!
Your answer
![](https://koobas.hobune.stream/wayback/20220613074621im_/https://answers.unity.com/themes/thub/images/avi.jpg)