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 Grengoshi · Jun 14, 2021 at 09:06 PM · healthbar

Cant seem to destroy UI GameObject ive instantiated after each hit

This code is probably really bad, and ive been trying different ways for it to work. I instantiate a heart UI game object in a UI Panel and each time an enemy hits the "player" im trying to remove a heart. I can seem to call the destroy method but only one heart is destroyed. Ive tried different ways to count down in the hit code but it seems that its the destroy(gameobject) that i might of messed up. Again im not good at this but i keep trying to practice. i gave up after 3 weeks of trying xD (sorry if this is not setup correctly i dont use this feature often)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Health_UI : MonoBehaviour
 {
 
 
     public int health;//how much health
     private int numOfHearts;//how many hearts
 
     public List<Image> hearts = new List<Image>();
     public Image spriteHeart;
     public Transform panelTranform;
     
     private GameObject heartsObject;
 
     private void Awake()
     {
         numOfHearts = health;
         HeartStartCaller();
     }
     void Start()
     {
     
     }
 
     
     void Update()
     {
         if (health == 0)
         {
             Time.timeScale = 0;
         }
     }
     private void HeartStartCaller()
     {
         for (int i = 0; i < numOfHearts; i++)
         {
             hearts.Add(spriteHeart);
 
 
         }
         foreach (var spriteHeart in hearts)
         {
             HeartHealth();
         }
     }
 
     private void HeartHealth()
     {
         heartsObject = (GameObject)Instantiate(spriteHeart.gameObject, panelTranform);
 
         //Instantiate(spriteHeart, panelTranform);
 
     }
     private void RemoveHealth()
     {
 
         Destroy(heartsObject);
         Debug.Log("Destroy");
     }
     public void TakeDamage(int damage)
     {
         //when this number changes it changes the health value
         health = health - damage;
         Debug.Log(health + "health");
         numOfHearts = health;// when health is 3 and u go to 2 it drops num hearts to 2
 
         for (int i = numOfHearts ; i < hearts.Count; i++)
         {
             hearts.Remove(spriteHeart);
             Debug.Log("Hearts count: " + hearts.Count);
 
             RemoveHealth();
         }
 
       
 
     }
     
 
 
  
 
 }
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

1 Reply

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

Answer by Hellium · Jun 14, 2021 at 09:45 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Health_UI : MonoBehaviour
 {
     [SerializeField] private GameObject heartPrefab;
     [SerializeField] private Transform heartsParent;
     [SerializeField, Min(1)] private int maxHealth = 10;
     private int health;

     // A Queue is a particular kind of "list"
     // https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm
     private Queue<GameObject> hearts = new Queue<GameObject>();
 
     private void Awake()
     {
         health = maxHealth;
         InstantiateHearts();
     }
 
     private void InstantiateHearts()
     {
         for (int i = 0; i < health; i++)
         {
             hearts.Enqueue(Instantiate(heartPrefab, heartsParent));
         }
     }
 
     public void TakeDamage(int damage)
     {
         health = Mathf.Max(health - damage, 0); // Ensures the health does not go below 0
         Debug.Log(health + "health");
 
         while(damage > 0 && hearts.Count > 0)
         {
             Destroy(hearts.Dequeue());
             damage--;
         }
         
         if (health == 0)
         {
             Time.timeScale = 0;
         }
     }
 }
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 Grengoshi · Jun 14, 2021 at 10:41 PM 0
Share

ill try it out mate thank you.

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

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

Related Questions

Splitscreen w/ different GUIs on each camera 2 Answers

Healthbar not always working, someone please help! 2 Answers

GUI.HorizontalScrollbar won't change it's height 1 Answer

GuiTexture above 3D objects 1 Answer

NullReferenceException: Object reference not set to an instance of an object 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