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 deglet · May 04, 2016 at 01:09 AM · enemycomponentdamageattackcomponents

My enemy don't take damage.

Hello to you ! I have a problem, my enemies refuse to die....(they are spawn and not already instanciate). I think it's because i can't get his current life from his script. But I tried everything and i can't get a component from "FindGameObjectsWithTag". Those are my script:

     public class s_PlayerControl : MonoBehaviour
     {
      public int FoeCurrentHealth;
     
      void Update()
         {
             GestureAttack();
             FoeCurrentHealth =   GameObject.FindGameObjectsWithTag("Foe").GetComponent<s_EnnemyControl>().FoeCurrentHealth;
         }
     
      GameObject GestureAttack()
         {
             invincible = true;
             GameObject[] Foes = GameObject.FindGameObjectsWithTag("Foe");
             GameObject closest = null;
             float distance = Mathf.Infinity;
             Vector3 position = transform.position;
     
           
             foreach (GameObject go in Foes)
             {
                 Vector3 diff = go.transform.position - position;
                 float curDistance = diff.sqrMagnitude;
                 if (curDistance < distance)
                 {
                     closest = go;
                     distance = curDistance;
                 }
             }
             if (recupgeste == "D" && recupgestescore > 0.60 || Input.GetKeyDown(KeyCode.Space))
             {
                 transform.position = Vector3.MoveTowards(transform.position, closest.transform.position, 100);
 
 //THE PROBLEM SOURCE (TOOK is log but not HIT)
                 s_PlayerControl enemyHealth = GetComponent<s_PlayerControl>();
                 Debug.Log(enemyHealth.FoeCurrentHealth);
                 if (enemyHealth.FoeCurrentHealth > 0)
                 {
                     Debug.Log("TOOK");
                     enemyHealth.TakeDamage(attackDamage);
                 }
             }
             return closest;   
         }
 
 }

And for the enemy

 public class s_EnnemyControl : MonoBehaviour
 {
  public int FoeCurrentHealth;
 public int startingHealth = 1;
 
  void Start()
     {
         FoeCurrentHealth = startingHealth;
     }
 
     public void TakeDamage(int amount)
     {
         Debug.Log("HIT");
         // If the enemy is dead...
         if (isDead)
             // ... no need to take damage so exit the function.
             return;
 
         FoeCurrentHealth -= amount;
 
         // If the current health is less than or equal to zero...
         if (FoeCurrentHealth <= 0)
         {
             // ... the enemy is dead.
             Death();
         }
     }
 
 }

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 deglet · May 04, 2016 at 09:20 AM

It's not my problem, they are tagged. I get their position previously. No, I can't damage them. Their life is staying at 1point whatever I do.

 GameObject GestureAttack()
     {
         invincible = true;
         GameObject[] Foes = GameObject.FindGameObjectsWithTag("Foe");
         GameObject closest = null;
         float distance = Mathf.Infinity;
         Vector3 position = transform.position;
 
 
         s_PlayerControl enemyHealth = GetComponent<s_PlayerControl>();
         foreach (GameObject go in Foes)
         {
             FoeCurrentHealth = go.GetComponent<s_EnnemyControl>().FoeCurrentHealth;
             Debug.Log(FoeCurrentHealth);
             Vector3 diff = go.transform.position - position;
             float curDistance = diff.sqrMagnitude;
             if (curDistance < distance)
             {
                 closest = go;
                 distance = curDistance;
             }
         }
         if (recupgeste == "D" && recupgestescore > 0.60 || Input.GetKeyDown(KeyCode.Space))
         {
             transform.position = Vector3.MoveTowards(transform.position, closest.transform.position, 100);
            // foreach (GameObject go in Foes)
             //{
                
                
                 Debug.Log(enemyHealth.FoeCurrentHealth);
             if (FoeCurrentHealth >0)
             {
                 Debug.Log("TOOK");
                 enemyHealth.TakeDamage(attackDamage);
             //}
             }
         }
         return closest;   
     }

As I say the TOOK is log, and it's log that they have 1 point of life. But the TakeDamage don't work. Even if I do enemyHealth.FoeCurrentHealth = enemyHealth.FoeCurrentHealth -1; They still have all their life.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get my TakeDamage script to hit/get other components? 0 Answers

How can I get my enemy to hurt/kill player 0 Answers

How to go on to next wave of enemies? 0 Answers

Damage not working properly 1 Answer

my enemy recognizes one instance of the player entering a box collider in order to apply damage but not to trigger it to chase the player 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