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 Satco.Brkic · Apr 03, 2013 at 10:47 AM · playerhealth

How to make a player die after health reaches 0

hey guys was just wondering how do i make my player die and respawn after the healthbar reaches 0

This is the code: using UnityEngine; using System.Collections;

public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; public int curHealth = 100;

 public float healthBarLengh;
 
 // Use this for initialization
 void Start () {
     healthBarLengh =  Screen.width / 2;
 
 }
 
 // update is called once per frame
 void update () {
     AddjustCurrentHealth(0);
     
 }
 
 void OnGUI() {
     GUI.Box(new Rect(10, 40, healthBarLengh, 20), curHealth + "/" + maxHealth);
     
 }
 public void AddjustCurrentHealth(int adj) {
     curHealth += adj;
     
     if(curHealth < 0)
         curHealth = 0;
     
     if(curHealth > maxHealth)
         curHealth = maxHealth;
     
     if(maxHealth < 1)
         maxHealth = 1;
     
     healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
 }

}

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 EliteMossy · Apr 04, 2013 at 04:59 AM 0
Share

Post your full script as it is now.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by EliteMossy · Apr 04, 2013 at 05:54 AM

Here is the fixed script.

 using UnityEngine;
 using System.Collections;
  
 public class PlayerHealth : MonoBehaviour {
      public int maxHealth = 100;
      public int curHealth = 100;
  
  
     public float healthBarLengh;
  
     // Use this for initialization
     void Start () {
        healthBarLengh =  Screen.width / 2;
  
     }
  
     // update is called once per frame
     private void update () {
        AddjustCurrentHealth(0);
  
     }
  
     private void OnGUI() {
        GUI.Box(new Rect(10, 40, healthBarLengh, 20), curHealth + "/" + maxHealth);
  
     }
 
     public void AddjustCurrentHealth(int adj) {
        curHealth += adj;
  
        if(curHealth < 0) {
             KillPlayer();
         }
         if(curHealth > maxHealth)
          curHealth = maxHealth;
 
          if(maxHealth < 1)
          maxHealth = 1;
  
        healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
     }
     
     public void KillPlayer() {
     // Play death animation etc.
     // Restart level or change position of player
     }
 }
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 EliteMossy · Apr 04, 2013 at 05:57 AM 0
Share

Sorry just edited, realized you completely screwed it up :D

avatar image Satco.Brkic · Apr 04, 2013 at 06:00 AM 0
Share

Thanks so much! Working fine! can probably tell im not the programmer in the project, one last question! how would i go about getting the player to respawn?

avatar image EliteMossy · Apr 04, 2013 at 06:08 AM 0
Share

Cool, don't forget to mark the correct answer as an answer. Also to respawn, you just instanitate from a prefab which you can drop in to your script.

Add this to the top of your script underneath public int curHealth = 100;

public GameObject PlayerPrefab;

Assign the prefab of the player to the Inspector in Unity.

Now change your $$anonymous$$illPlayer code to this:

  public void $$anonymous$$illPlayer() {
      Instaniate(PlayerPrefab, transform, rotation);
      Destroy(gameObject);
     } 

Should work, but i would look at some other resources. This is not the way i would do it, and not 100% sure if it is a good way.

avatar image
2

Answer by save · Apr 03, 2013 at 10:54 AM

You already have the (curHealth < 0) statement, call for a function inside that.

 if(curHealth < 0) {
     curHealth = 0;
     KillPlayer();
 }
 
 public void KillPlayer() {
     // Play death animation etc.
     // Restart level or change position of player
 }
Comment
Add comment · Show 6 · 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 LyanApps · Apr 03, 2013 at 02:09 PM 0
Share

To respawn you could just create a prefab of you player and Instantiate a new GameObject at some waypoint. http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

avatar image Satco.Brkic · Apr 04, 2013 at 03:46 AM 0
Share

$$anonymous$$eep getting a 'Unexpected Symbol 'public' nothing seems to fix it

avatar image joeyaaaaa · Apr 04, 2013 at 04:02 AM 0
Share

that could mean a missing bracket

avatar image joeyaaaaa · Apr 04, 2013 at 04:09 AM 0
Share
 public class PlayerHealth : $$anonymous$$onoBehaviour { public int maxHealth = 100; public int curHealth = 100;
 public float healthBarLengh;
 
 //} you need a bracket right here
 // Use this for initialization
 void Start () {
     healthBarLengh =  Screen.width / 2;
  
 }
avatar image EliteMossy · Apr 04, 2013 at 04:29 AM 0
Share

@joeyaaaaa How would that fix it?

Show more comments
avatar image
0

Answer by joeyaaaaa · Apr 04, 2013 at 04:46 AM

Actually i think you are declaring new pubs(vars) in a wrong place(ithink)but just add that bracket i placed within my comments where i put it if it still doesnt work move those publics to the beginning of script or add the bracket to the end, sprry its hard to tell whats wrong or where brackets go sometimes edit: i read wrong sorry i read"how would u fix it lmao any ways im sure its where your publics

Comment
Add comment · Show 7 · 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 EliteMossy · Apr 04, 2013 at 04:58 AM 0
Share

Yeah, but he needs to post his full script so we can see.

avatar image joeyaaaaa · Apr 04, 2013 at 05:01 AM 0
Share

Lol would only help

avatar image Satco.Brkic · Apr 04, 2013 at 05:40 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : $$anonymous$$onoBehaviour {
      public int maxHealth = 100;
      public int curHealth = 100;
     
     
     public float healthBarLengh;
     
     // Use this for initialization
     void Start () {
         healthBarLengh =  Screen.width / 2;
     
     }
     
     // update is called once per frame
     void update () {
         AddjustCurrentHealth(0);
         
     }
     
     void OnGUI() {
         GUI.Box(new Rect(10, 40, healthBarLengh, 20), curHealth + "/" + maxHealth);
         
     }
     public void AddjustCurrentHealth(int adj) {
         curHealth += adj;
         
         if(curHealth < 0) {
     curHealth = 0;
     $$anonymous$$illPlayer();
 }
  
 public void $$anonymous$$illPlayer() {
     // Play death animation etc.
     // Restart level or change position of player
 }
         
         if(curHealth > maxHealth)
             curHealth = maxHealth;
         
         if(maxHealth < 1)
             maxHealth = 1;
         
         healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
     }
 }
avatar image joeyaaaaa · Apr 04, 2013 at 05:45 AM 0
Share

ya the bracket i told you to add probably shouldnt be there, i know pretty much only java but i see public class PlayerHealth : $$anonymous$$onoBehaviour { should connect to a bracket at the end of your script, i believe so and i believe these>

     public int maxHealth = 100;
      public int curHealth = 100;
  
  
     public float healthBarLengh; should come first but after this>using UnityEngine;
 using System.Collections;
 again i am unsure

but i am sure that

      public void AddjustCurrentHealth(int adj) {
        curHealth += adj;
  
     if(curHealth < 0) {
     curHealth = 0;
     $$anonymous$$illPlayer();
 }

you need another bracket at the end of this function^^^^^^^^^^^^

avatar image joeyaaaaa · Apr 04, 2013 at 05:48 AM 0
Share
 public void $$anonymous$$illPlayer() {
     // Play death animation etc.
     // Restart level or change position of player
 /*}
  
 }*/
  ^^^^^^and for now remove these two brackets, they are stopping the code from seeing the restVVVVVVVVVVVVV
  
  
        if(curHealth > maxHealth)
          curHealth = maxHealth;
  
        if(maxHealth < 1)
          maxHealth = 1;
  
        healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
     }
 }
Show more comments

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

14 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

health bar script please help 2 Answers

Damage taking? 1 Answer

Having trouble with player.transform 1 Answer

Unity Effect 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