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 2-Fraimz · Apr 18, 2014 at 12:45 PM · prefabenemydamagemultiple objectshealth

How to have an NPC with multiple parts detect damage

I have an enemy NPC built from several simple game objects (cubes, cylinders, etc.). I have followed a tutorial in making enemies take damage if they're within a certain range, and the code works fine on single objects. I have tried both placing all of the objects into an empty object and having them saved as a prefab, then applying the code to them, but when I play the game and attack the enemy, the health remains unchanged.

Code:

Attack code within Main character: #pragma strict

 var Damage:int = 25;
 var Distance:float;
 var enemyDistance:float = 2;
 
 function Update()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         var hit:RaycastHit;
         
         if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
         {
             Distance=hit.distance;
             
             if(Distance<enemyDistance)
             {
                 hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
 }

Damage code within enemy:

 #pragma strict
 
 var enemyHealth:int = 100;
 
 function Update ()
 {
     if (enemyHealth<1)
     {
         Dead();
     }
 }
 
 function ApplyDamage (Damage:int)
 {
     enemyHealth -= Damage;
 }
 
 function Dead()
 {
     Destroy (gameObject);
 }


Although I am relatively new to Unity, as far as I'm aware, this should work. Any help would be greatly appreciated.

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 Ed unity · Apr 18, 2014 at 02:34 PM

In your case, you only want to have the enemy script attached to the Root Game object that every other piece of the enemy is under. So you said that you made all other objects a child of an empty game object. You should only need to attach the script to the empty GameObject and then that object will be the one you need to access in order to call the functions and apply damage.

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

21 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

Related Questions

The enemy don´t lose health, but why??? 0 Answers

Prefab shooting damage/health? 1 Answer

Melee Damage script by collision 2 Answers

how do i damage the player on enemy start, Not collision 2 Answers

Health below zero but shows negative numbers 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