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 /
  • Help Room /
avatar image
0
Question by YoloJoe · Sep 05, 2016 at 01:20 AM · health barhealth

My enemy is not taking damage...

I've tried to wrap my head around this for too long, so I'm just going to ask here :)

I've made an Enemy Script that's attached to the enemy in the scene: (The enemy script is called EnemyTest.cs and the enemy in the scene is called enemyPrefab.)

       [code]
      using UnityEngine;
      using System.Collections;
       using UnityEngine.UI;


  public class EnemyTest : MonoBehaviour {

 private int health, maxHealth;
 public Image healthBar;

 void Start ()
 {
     healthBar = transform.FindChild ("EnemyCanvas").FindChild ("enemyHealthBG").GetComponent<Image> ();
     health = 100;
     maxHealth = 100;
 }

 void Update () {
 
 }

 public void Hit(int damage)
 {
     health -= damage;
     healthBar.fillAmount = (float)health / (float)maxHealth;
 }
  }

   [/code]

In my bullet prefab, I have this script attached. Bullet1.cs:

     [code]

      using UnityEngine;
       using System.Collections;

    public class Bullet1 : MonoBehaviour {
 
 float speed;

 void Start () {
     Destroy (this.gameObject, 2);
     speed = 15;
     
 }


 void Update () {
     transform.position += transform.forward * Time.deltaTime * speed;
 }

 void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.tag == "Enemy") 
     {
         other.gameObject.GetComponent<EnemyTest> ().Hit (10);
         Debug.Log ("Hit enemy");
         Destroy (this.gameObject);
     }
 }
     }

  [/code]

The bullet prefab does not have a tag, but the enemy is tagged as "Enemy".

I have an enemy Canvas attached to the enemy too, with the health sliders and everything. Why is this not working? I followed a tutorial on youtube (https://www.youtube.com/watch?v=5zNDRMJ3_AA)

Any help would be REALLY appreciated. Cheers.

Comment
Add comment · Show 2
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 Benjames · Sep 05, 2016 at 01:52 AM 0
Share

Does the Debug.Log("Hit enemy") show up?

Try making health a public variable so you can see if it changes in your scene.

Also the RigidBody on the bullet might need different collision settings because if it's moving too fast it might not recognize the collision. Try slowing it down.

avatar image YoloJoe · Sep 05, 2016 at 07:35 AM 0
Share

Yeah thats the thing.. Te Debug.Log does show up. So the hit is being registred.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iagoccampos · Sep 05, 2016 at 02:14 AM

There are many factors that prevent "OnCollisionEnter " to be called... if the bullet is too fast, as @Benjames says, it will "jump" over the enemy collider. Check the layer collisison matrix on Edit > Project Settings > Physics. I don't know how is your game mechanic but take a look on OverlapSphere for your bullet or Raycast to player gun...

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 YoloJoe · Sep 05, 2016 at 07:40 AM 0
Share

As I commented on @Benjames' post, the hit is being registred by the Debug.Log. So it should work? I dont really want to use RayCast as Im making a tower defense.

avatar image
0

Answer by Dream_in_code · Sep 05, 2016 at 03:05 AM

//Try using OnTriggerEnter2D.Make sure the enemy box collider is triggered.

void OnTriggerEnter2D(collider2D other)

{

if (other.gameObject.tag == "Enemy")

  {
      other.gameObject.GetComponent<EnemyTest> ().Hit (10);
      Debug.Log ("Hit enemy");
      Destroy (this.gameObject);
  }

}

}

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 YoloJoe · Sep 05, 2016 at 07:37 AM 0
Share

Ok, i will try this. I.m making a 3D game though.. Does that matter when using onTriggerEnter2D?

avatar image
0

Answer by YoloJoe · Sep 05, 2016 at 07:46 AM

Would it be better to have the OnCollisionEnter statement in the EnemyTest.cs? And tag the bullet as "Bullet1" or something?

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 YoloJoe · Sep 05, 2016 at 09:38 AM 0
Share

I found the issue. The enemy IS taking damage, however the healthBar is not decreasing. Anybody got a solution to this?

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

54 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

Related Questions

Health bar help 1 Answer

How would you tie a keypress to a fill amount? 1 Answer

I have a jump scare and I want it to affect my player's health when it activates it and kill my player when health = 0. 0 Answers

Help with a health bar 0 Answers

Health bar/damage script not working. Please help. 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