Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JBoy · Jul 24, 2012 at 09:32 AM · crashloadlevelhitpoints

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);
  }
 }


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JBoy · Jul 25, 2012 at 01:01 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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); } }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JBoy · Jul 25, 2012 at 12:47 AM 0
Share

Thanks for your help, but unfortunately it doesn't work! - I guess I will need to think of another way to respawn.

avatar image
1

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;
  }
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BeHappy · Jul 24, 2012 at 10:14 AM 0
Share

So when hitPoints comes to 100, update function will execute the statement "Application.LoadLevel(Application.loadedLevel);" redundantly and cause Unity to crash.

avatar image Kryptos · Jul 24, 2012 at 12:38 PM 0
Share

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);
avatar image JBoy · Jul 25, 2012 at 12:48 AM 0
Share

Even if I only call it for one frame, it will still crash ($$anonymous$$y $$anonymous$$d is boggled)!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player crash during the loading of a level 1 Answer

LoadLevelAsync Causes Unity to Crash? 1 Answer

Game crashes on Loadlevel when called on a swipe event 1 Answer

Crash on LoadLevel (Android Project) 1 Answer

Giant spike with level load causes crash with iphone 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges