• 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 bscohen06 · Jan 13 at 08:32 PM · destroyenemyhealth

health code help

I was programming this health code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class hurt : MonoBehaviour
 {
     public GameObject img;
     public GameObject img1;
     public GameObject img2;
     int h = 0;
     
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         switch (h)
         {
             case 1:
                 Destroy(img);
                 break;
             case 2:
                 Destroy(img1);
                 break;
             case 3:
                 Destroy(img2);
                 break;
         }
 
     }
     void OnCollisionEnter(Collision other)
     {
     if (other.gameObject.CompareTag("e"))
     {
         h = h + 1;
             
 
 
         }
         else
     {
         h = h + 0;
     }
 
 } 
 
 }

This code deletes hearts when I touch the enemy. but the order that the hearts delete is very inconsistent like sometimes it will delete the middle heart then last heart and not the first sometimes it will delete the first two but not the third. Then when I touch it again deletes the third why is this so inconsistent.

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
0

Answer by underkitten · Jan 13 at 10:04 PM

There is a possibility that multiple colliders enter at the same time and you "h" becomes 2 or even 3 after a touch. Try something like this: Instead of having 3 GameObjects, make a list, this will be useful if you decide to have more health. In Editor you can adjust how many hearts you want to have an slot them in in order (instead of slotting them in you can also Instantiate them but that's a different question). With each touch (collide) method Hit() will be called out and it will Destroy 1st heart in the list and also clear out 1st entry.

 public List<GameObject> hearts = new List<GameObject>();
 
     private void Hit()
     {
         Destroy(hearts[0]);
         hearts.Remove(hearts[0]);
     }
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "e")
         {
             Hit();
         }
     }

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 underkitten · Jan 13 at 10:05 PM 0
Share

I would avoid putting stuff like Instantiate/Destroy etc in the Update() function because it checks for it every frame.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Enemy Health Problems 1 Answer

Enemy health bar over them and destroy actor 0 Answers

Enemy health and bullet problem 2 Answers

How do I destroy a game object (The enemy Goblin Game Object) upon entering a collision box? (JavaScript) 2 Answers

Unity health problem and enemy colision 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges