Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 m0mohy92 · Jun 15, 2019 at 06:33 PM · collision detectionloadlevelhealth-deductionhealth

How to make heart health system?

Hi everyone! I am trying to make 3 lives for the player, each time a ball hit him, hearts decrease and disappear till the end of the game. I have two problems:

  1. When the ball hit the player, hearts still remaining no one disappear

  2. I am using LoadLevel, so when some heart disappear it appear again when reloading the level, how to not restore hearts total amount. I tried DontDestroy function, but didn't work for me. Here is few lines of the code


  void CheckHealth() {
             switch (Health) {
             case 3:
                 h1.gameObject.SetActive (true);
                 h2.gameObject.SetActive (true);
                 h3.gameObject.SetActive (true);
                 break;
             case 2:
                 h1.gameObject.SetActive (true);
                 h2.gameObject.SetActive (true);
                 h3.gameObject.SetActive (false);
                 //Health= PlayerPrefs.SetInt("Health", 2);
                 break;
             case 1:
                 h1.gameObject.SetActive (true);
                 h2.gameObject.SetActive (false);
                 h3.gameObject.SetActive (false);
                 //Health= PlayerPrefs.SetInt("Health", 1);
                 break;
     
             case 0:
                 h1.gameObject.SetActive (false);
                 h2.gameObject.SetActive (false);
                 h3.gameObject.SetActive (false);
                 gameover.gameObject.SetActive (true);
                 //Health= PlayerPrefs.SetInt("Health", 0);
                 Time.timeScale = 0;
                 break;
             }
     
         }
     
     
     void OnTriggerEnter2D(Collider2D col) {
             
             string[] name = col.name.Split ();
             if (name.Length > 1) {
                 if (name [1] == "Ball") {
                     Health--;
                     CheckHealth ();
                         StartCoroutine (KillThePlayer ());
     
                     }
                 
                 }
             }
 
 
 
 
 
 

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Meishin · Jun 15, 2019 at 07:54 PM

Hello @m0mohy92,

For your 1st issue ; How are defined h1, h2, h3 ? If those are UI images (attached to an UI canvas) then it should work with "hN.enabled (false);" (remove the ".gameObject")

For the 2nd issue (passing a variable from scene to scene) you can use DontDestroyOnLoad() as you said, tho you need your script to be either a monobehavior derived class or attached to a gameObject ;

 public void Awake()
 {
    DontDestroyOnLoad(this.gameObject);
 }

Of course if your script is on the player itself and you destroy him when you exit your scene it's not gonna work (simply put the UI update in another script / on an other empty gameobject)

All in all your script should look like ;

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;

 // Attach to an empty GameObject
 // To initialize script on a new scene, add updateHealthUI() in the Awake or Start Method of your player
 // Then just use this script in your OnCollision method using thisScript.Health --
 public class UpdateHealthUI : MonoBehaviour // MonoBehaviour
 {
 
 // Insert your 3 hearts images in the Unity Editor
 [SerializeField] private Image h1, h2, h3;

 // Create an array because we're lazy
 private Image[] images;

 // Gameover
 [SerializeField] private Image gameOver;

 // A private variable to keep between scenes
 [SerializeField] private int health = 3;

 // Now we define Get / Set methods for health
 // In case we Set health to a different value we want to update UI
 public int Health { get { return health; } set { if (health != Health) health = Health;  updateHealthUI(); } }
 
 public void Awake()
 {
     DontDestroyOnLoad(this.gameObject);
     images = new Image[] { h1, h2, h3 };
 }

 private void updateHealthUI()
 {
     for(int i = 0; i < images.Length;i++)
     {
         // Hide all images superior to the newHealth
         if (i >= health)
             images[i].enabled = false;
         else
             images[i].enabled = true;
     }
     // Game Over
     if (health == 0)
     {
         gameOver.enabled = true; ;
         Time.timeScale = 0;
     }
 }
 }
Comment
Add comment · 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
0

Answer by EDevJogos · Jun 15, 2019 at 08:05 PM

You can use void OnSceneLoaded(Scene scene, LoadSceneMode mode) to check when a level is loaded and then call your method CheckHealth(); inside. Note that Health would have to be declared as static so that it's value persist during change of scenes.

As for why the hearts are not disapearing when a ball hits the player i don't know, is hard to say by just looking at the piece of code you provided, the best i can say to you is to Debug, see if the object name is actually what you expect and things like that.

For More info on the SceneLoaded callback check: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

Comment
Add comment · 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
0

Answer by Constantin_d · Jun 10, 2020 at 11:29 AM

This is a really easy way to do it. If you like, check it out.

https://www.youtube.com/watch?v=Dg-6qoZ2JqI&list=PLyz_wKnxtoQO0Tf3koreC4gJtFv4213yQ

Comment
Add comment · 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

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

113 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 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 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 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 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 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 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 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 avatar image

Related Questions

How to make player keep getting hurt when touching fire? 1 Answer

Player health dropping far to fast 1 Answer

Spawing small sprites proportionate to health 0 Answers

how do u make diffrent chacters and give them commands 2 do things 2 Answers

how to rightly detect a punch and manage health in a 3d fight/combat type game? 0 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