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 Ectogenesis · Jan 14, 2018 at 09:33 PM · enemy health

Enemy script only runs for one enemy

Hello everyone.Im beginner and trying to make 2d platformer.I have little issue here. Here is my enemy script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour {
     public int health;
     private int maxHealth = 100;
     private float distance;
     public Transform targetTransform;
     public bool canTakeDamage;
     // Use this for initialization
     void Start () {
         health = maxHealth;
     }
     void Update () {
         calculateDistance();
     }
     void calculateDistance()
     {
         distance = Vector3.Distance(gameObject.transform.position, targetTransform.transform.position);
         if(distance < 1)
         {
             canTakeDamage = true;
         }else if(distance > 1)
         {
             canTakeDamage = false;
         }
     }
     public void takeDamage(int damage)
     {
         health = health - damage;
 
     }
 }

and here is my playerscript:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
     private Rigidbody2D rgbd2d;
     private Animator anim;
     private Enemy enemy;
     public float speed;
     public float jumpForce;
     public bool isGrounded=true;
     public float maxSpeed;
     public bool isWalking;
     private float move;
     public bool isEnemyAgainstMe;
    
     // Use this for initialization
     void Start () {
         rgbd2d = gameObject.GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
         enemy = GameObject.FindGameObjectWithTag("enemy").GetComponent<Enemy>();
     }
     
     // Update is called once per frame
     void Update () {
         anim.SetFloat("speed", Mathf.Abs(move));
         anim.SetBool("isGrounded", isGrounded);
         attack();
         amIWalking();
         doIWantToJump();
         determinePlayersDirection();
       
     }
     void FixedUpdate()
     {
         move = Input.GetAxis("Horizontal");
         rgbd2d.velocity = new Vector2(move * speed, rgbd2d.velocity.y);
         if(rgbd2d.velocity.x > maxHiz)
         {
             rgbd2d.velocity = new Vector2(maxHiz, rgbd2d.velocity.y);           
         }
         else if (rgbd2d.velocity.x < -maxHiz)
         {
             rgbd2d.velocity = new Vector2(-maxHiz, rgbd2d.velocity.y);
         }
  
     }
    
     void amIWalking()
     {
         if (rgbd2d.velocity.x < 0 || rgbd2d.velocity.x>0)
         {
             isWalking = true;
         }
         else if(rgbd2d.velocity.x == 0)
         {
             isWalking = false;
         }
     }
     void determinePlayersDirection()
     {
         if (Input.GetAxis("Horizontal") < 0)
         {
             transform.localScale = new Vector3(-1, 1, 1);
         }
         else if (Input.GetAxis("Horizontal") > 0)
         {
             transform.localScale = new Vector3(1, 1, 1);
         }
     }
     void doIWantToJump()
     {
         if (Input.GetButtonDown("Jump"))
         {
             if (isGrounded)
             {
                 rgbd2d.AddForce(Vector2.up * jumpForce);
                 isGrounded = false;
             }
         }
     }
     void attack()
     {
         if (Input.GetKeyDown("return"))
         {
             if (enemy.canTakeDamage == true && isEnemyAgainstMe == true)
             {
                 enemy.takeDamage(20);
             }
         }
     }
 }

The issue is, when i add two or more enemies, the enemy script only runs at last added enemy. Where am i doing wrong?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Divinyx · Jan 14, 2018 at 10:57 PM

I'm not sure what you mean by

"the enemy script only runs at last added enemy."

do the enemy variables not initialize or update? Are the components not being attached to the enemy?


I'm gonna assume you want the enemy to take damage since you also added your player script in there, thing is... you're only accounting for one single enemy

 private Enemy enemy;
 enemy = GameObject.FindGameObjectWithTag("enemy").GetComponent<Enemy>();
 if (enemy.canTakeDamage == true && isEnemyAgainstMe == true)
      {
            enemy.takeDamage(20);
      }

so you might want to use a collection to store all enemies and use GameObject.FindGameObjectsWithTag("enemy") not Object.. note the 's'

then reference it with an index such as enemy[0].canTakeDamage ...

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

Answer by Ectogenesis · Jan 16, 2018 at 09:11 PM

Thanks for support! I m getting an idea about process. I updated my player script and still can t decrease enemies health independently(randomly only one enemy can take damage and the others cant).Im sharing only updated parts. Player script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
     private Rigidbody2D rgbd2d;
     private Animator anim;
     public Enemy enemy;
     public float speed;
     public float jumpForce;
     public bool isGrounded=true;
     public float maxHiz;
     public bool isWalking;
     private float move;
     private GameObject[] enemies;
     private int enemyIndex;
 
     //public bool enemyClose;
     public bool isEnemyAgainstMe;
 
     // Use this for initialization
     void Start () {
         rgbd2d = gameObject.GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
         enemies = GameObject.FindGameObjectsWithTag("enemy");
     }
 public void takeIndex(int index)
     {
         enemyIndex = index;
     }
     void attack()
     {
         if (Input.GetKeyDown("return"))
         {
             enemy = enemies[enemyIndex].GetComponent<Enemy>();
             if (enemy.canTakeDamage == true && isEnemyAgainstMe == true)
             {
                
                 enemy.takeDamage(20);
             }
         }
     }

And here is my enemy script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour {
     public int index;
     public int health;
     private int maxHealth = 100;
     private float distance;
     public Transform targetTransform;
     private PlayerController player;
     //public float distanceLimit;
     public bool canTakeDamage;
 void calculateDistance()
     {
         distance = Vector3.Distance(gameObject.transform.position, targetTransform.transform.position);
         if(distance < 1)
         {
             canTakeDamage = true;
             player.takeIndex(index);
         }else if(distance > 1)
         {
             canTakeDamage = false;
         }

So i gave manually indexes to the enemies but im sure there is an easier way to do.How can i do this? Thanks for the helps again :)

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 Divinyx · Jan 16, 2018 at 09:32 PM 0
Share

Its random because if you set the enemy index manually.. its not the same as indices co$$anonymous$$g from GameObject.FindGameObjectsWithTag("enemy"). Right now you don't have anything to check what enemy you are looking at, so you could only just either choose to damage every enemy near the player or damage one random enemy around the player and ignore the rest. Ins$$anonymous$$d of setting the indices manually, just loop through all possible enemies and check which ones are nearby, then damage those...

 foreach (Enemy enemy in enemies) {
    // if enemy is closeby
    enemy.takeDamage(20)
 }

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

74 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

Related Questions

SendMessage is working but the variable is not being updated on the other end 1 Answer

OnTriggerEnter 1 Answer

how to kill an emeny? 1 Answer

I have been watching his tutorial but nothing happens 0 Answers

[Answered](C#) Help with dealing damage to enemy from Raycast 3 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