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 jorgegb1997 · Sep 11, 2014 at 10:01 AM · loadlevelhplose

Load level when hp = 0

Hello. I have a simple combat game with 2 oponents but when the life bar is = to "0" nathing happens. I Want that when life bar of "bad" character = to 0 load a new level.

HP SCRIPT:

using UnityEngine;

using System.Collections; /// /// HP & MP Manager /// public class HpManager : MonoBehaviour { // HP bar, MP bar public UISlider hpBar, mpBar; // Max State public int hpMax = 100; public int mpMax = 100; // Current State int hp = 100; int mp = 100;
// Init HP State public void InitHp() { SetHp(hpMax); } // Init MP State public void InitMp() { SetHp(mpMax); } // Set Damage on HP State public void DoDamageHp(int point) { SetHp(hp - point); } // Set Recover on HP State public void DoSaveHp(int point) { SetHp(hp + point); } // Set Recover on MP State public void DoSaveMp(int point) { SetMp(mp + point); } // Set HP State public void SetHp(int point) { hp = Mathf.Clamp(point, 0, hpMax); if (hpBar) hpBar.value = (float)hp / (float)hpMax; } // Set MP State public void SetMp(int point) { mp = Mathf.Clamp(point, 0, mpMax); if (mpBar) mpBar.value = (float)mp / (float)mpMax; } }
Comment
Add comment · Show 4
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 Malfegor · Sep 11, 2014 at 10:25 AM 0
Share

You might want to try something like:

 public void SetHp(int point)
     {
         hp = $$anonymous$$athf.Clamp(point, 0, hp$$anonymous$$ax);
         if (hpBar)
             hpBar.value = (float)hp / (float)hp$$anonymous$$ax;
     
         if(transform.tag == "BadGuy" && hp <= 0)
         {
             // The following will reload the current level! 
             // $$anonymous$$ake sure you add it as a build scene.
             Application.LoadLevel(Application.loadedLevel);
         }
     
     }

Just make sure your opponent has the "BadGuy" tag!

P.S. There are better ways to do this, but this is probably the fastest way

avatar image jorgegb1997 · Sep 11, 2014 at 05:04 PM 0
Share

ALL THE SCRIPTS ARE WOR$$anonymous$$ING, WHEN THE BAD AND GOODGUY LOSE LIF$$anonymous$$

THAN$$anonymous$$S A LOT!

THAN$$anonymous$$ YOU :))))))

avatar image jorgegb1997 · Sep 11, 2014 at 05:12 PM 0
Share

O$$anonymous$$G! Finnallly!!

Thank you so much for your help!

It works!

Thanks a lot!

avatar image orb · Sep 12, 2014 at 09:32 AM 0
Share

You should accept the correct answer so this question turns a delightful green ;)

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

how to link objects in the inspector on load level 1 Answer

connect to hosted level 0 Answers

Loadlevel can't see each other. 2 Answers


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