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 Asukakitti09 · Dec 08, 2013 at 09:42 PM · aicombatscriphealth

Enemy AI won't take Damage

I have a simple combat script set up, but everytime the prefab hits the enemy the enemies health will not go down. I could use any help I'm at the verge of giving up on this I can't figure it out.

 public var path1 : Transform;
 public var path2 : Transform;
 
 public var enemySpeed : int = 6;
 
 public var currentHealth : int = 1;
 public var maxHealth : int  = 1;
 
 private var target : Transform;
 private var controller : CharacterController;
 
 function Start () : void 
 {
     SetTarget(path1);
     controller = GetComponent(CharacterController);
     currentHealth = maxHealth; 
     gameObject.renderer.material.color = Color.red;
 }
 
 function SetTarget(newTarget : Transform) : void
 {
     target = newTarget;
 
 }
 
 function Update () : void
 {
     var lookAtPosition : Vector3 = new Vector3 (target.position.x, this.transform.position.y, target.position.z);
     transform.LookAt(lookAtPosition);
     controller.SimpleMove(transform.forward*enemySpeed);
 
 }
 
 function OnTriggerEnter (node : Collider) : void
 {
     if (node.transform == target)
     {
         if (target == path1)
 
             SetTarget(path2);
                 
         else if (target == path2)
             SetTarget(path1);
 
 }        
 }
 
 function ModifyHealth (change : int) : void 
 {
 {
     currentHealth += change;
     if (currentHealth > maxHealth)
         currentHealth = maxHealth;
     else if (currentHealth <= 0)
             Destroy(this.gameObject);
         
 
 }    
 }
 
 
 
 this is the bullet script
 
 var shot : GameObject;
 function Start()
 {
 }
 
 function OnCollisionEnter(info : Collision) : void {
     if(info.collider.tag == "Enemy"){
         info.collider.SendMessageUpwards("ModifyHealth", -1);
     }
 }
 function Update () {
 if(Input.GetButtonDown("Fire1"))
 {
 var porjectile = Instantiate(shot, transform.position, Quaternion.identity);
 porjectile.rigidbody.AddForce(transform.forward * 1500);
 }
 }


Comment
Add comment · Show 7
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 meat5000 ♦ · Dec 08, 2013 at 09:44 PM 0
Share

Have you tried taking out the void function types? I don't think they are required.

$$anonymous$$aybe, not relevant :P

In any case, you will need to add in a whole bunch of

 Debug.Log("Stuff" + variables);

to see whats going on.

avatar image Asukakitti09 · Dec 08, 2013 at 10:06 PM 0
Share

I don't understand could you please show me an example?

avatar image meat5000 ♦ · Dec 08, 2013 at 10:08 PM 0
Share
 function OnCollisionEnter(info : Collision) {
      Debug.Log("Collision Detected");
      if(info.collider.tag == "Enemy"){
      info.collider.Send$$anonymous$$essageUpwards("$$anonymous$$odifyHealth", -1);
      Debug.Log("Enemy Hit");
      }
 }
avatar image Commander Quackers · Dec 08, 2013 at 10:11 PM 0
Share

Use Debug.Log as stated above to check whether or not the prefab actually does anything + make sure your colliders are set to trigger

avatar image HuskyPanda213 · Dec 08, 2013 at 10:18 PM 0
Share

Your adding health, not subtracting it.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ozone · Dec 08, 2013 at 11:35 PM

Perhaps try using info.gameObject.SendMessageUpwards() instead of info.collider.SendMessageUpwards(). That should ensure that the message reaches all scripts attached to the enemy game object. You will also need to use if(info.gameObject.tag == "Enemy") instead of what you have now, because tag is not a member of collider.

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

Multiple Cars not working 1 Answer

How can an enemy deplete players health? 0 Answers

ZOMBIE AI SCRIPT 1 Answer

AI script not using gravity 1 Answer

Player Health drops fast, how to pause after damage 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