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 crawniik · Jun 11, 2018 at 05:53 AM · instantiateprefabshootingdamagehealth

Prefab shooting damage/health?

I can't seem to get my damage and health system working. If someone could take a look and tell me what I'm doing wrong, it would be greatly appreciated.

This is my Health.cs script that I attach to things I want to be able to kill.

 using UnityEngine;
 
 public class Health : MonoBehaviour {
 
     public int maxHealth = 100;
     public int currentHealth;
     public bool alive = true;
 
     public void Start()
     {
         currentHealth = maxHealth;
     }
     public void TakeDamage(int damageAmount)
     {
         if(currentHealth < 1)
         {
             alive = false;
             Destroy(this.gameObject);
         } else
         {
             alive = true;
             currentHealth -= damageAmount;
         }
     }
 }  

This is my damage script that I currently have attached to the projectile that will be colliding with the enemies.

 using UnityEngine;
 
 public class Damage : MonoBehaviour {
 
     public int damageAmount = 10;
 
     public void OnCollisionEnter(Collision col)
     {
         if(col.rigidbody)
         {
             col.rigidbody.GetComponent<Health>().TakeDamage(damageAmount);
         }
     }
 }



Thanks in advance to anyone that lends me a hand.

Comment
Add comment · Show 5
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 SlowCircuit · Jun 11, 2018 at 06:00 AM 0
Share

What is the issue you're having?

avatar image crawniik SlowCircuit · Jun 11, 2018 at 06:05 AM 0
Share

It isn't taking health away from the enemy when the projectile hits it.

avatar image crawniik · Jun 11, 2018 at 04:05 PM 0
Share

to elaborate on my code and what I've tried, this is what my Damage.cs script was originally, but the one I posted above is what I changed it to while trying to get this to work.

 using UnityEngine;
 
 public class Damage : $$anonymous$$onoBehaviour
 {
 
     public int damageAmount = 10;
 
     public void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "Collide")
         {
             Health _health = col.gameObject.GetComponent<Health>();
             _health.TakeDamage(damageAmount);
         }
     }
 }


The object I am trying to take health away from is tagged with the "Collide" tag. I'm not understanding why this isn't working. Someone that knows something about Unity, please help me. @thetomfinch

avatar image Kek_Kek · Jun 11, 2018 at 06:47 PM 0
Share

What if you try using OnTriggerEnter() ins$$anonymous$$d of On Collision Enter

avatar image crawniik Kek_Kek · Jun 11, 2018 at 07:03 PM 0
Share

I'll give that a shot, thanks for your input.

1 Reply

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

Answer by bakir-omarov · Jun 11, 2018 at 06:51 PM

Hey. Let's say you have Enemy and Player game objects on scene.


1) Make sure you attached Health to Player game object.
2) Make sure you have Collider and Rigidbody on Player.
3) Make sure your Player has a tag Player.
4) Make Sure you have attached Damage script to Enemy game object.
5) Make sure you have Collider (uncheck "is trigger", we don't need trigger for now) and Rigidbody (uncheck "is kinematic",because you can't trigger oncollisionenter if you are kinematic) on Enemy.
And try this code:


     public void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.CompareTag("Player")) // it is similar gameObject.tag == "Player" 
         {
             Debug.Log("I have collision with Player! Trying to give him some damage!");
             Health _health = col.gameObject.GetComponent<Health>();
             if (_health != null)
                 _health.TakeDamage(damageAmount);
         }
         else Debug.Log(col.gameObject.name);
     }


Comment
Add comment · Show 6 · 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 crawniik · Jun 11, 2018 at 07:02 PM 0
Share

I'm not sure I follow. I understand the code you posted, but your instructions aren't quite right for me.

What I'm trying to do at the moment is just allow the prefab bullet that $$anonymous$$Y player is shooting to do damage to the enemy. Not the other way around.

I have the damage script attached to my prefab bullet. I have the health script attached to my "enemy" is that not the way I should have it set up? Also, which "is trigger" is supposed to be unchecked? Is the the one on the prefab bullet or the one on the "enemy"?

I appreciate your help.

avatar image bakir-omarov crawniik · Jun 11, 2018 at 07:06 PM 0
Share

Just screenshot and send here:

1) Your bullet prefab, i need to see all details in inspector
2) Your Enemy game object.

So i can help you in fastest way!

avatar image crawniik bakir-omarov · Jun 11, 2018 at 07:17 PM 0
Share

alt text

paintball.png (68.6 kB)
enemy.png (59.0 kB)
Show more comments

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

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

Related Questions

AddForce not working on instantiated bullets 1 Answer

Enemies sharing health 2 Answers

Script for enemy health loss on contact with bullet prefab 1 Answer

take collision info from an instantiated bullet 1 Answer

Enemy Health Damage 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