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 VColson · Feb 06, 2017 at 03:11 PM · c#2d2d-platformerenemyenemy ai

How to have multiple enemies (currently having to kill them in order)

I'm making a 2D platformer and I made an enemy that shoots the player when it is in a certain range, turns when player is on the other side and has an amount of health. But when I add a second enemy (I did this by duplicating) and try to kill that one first the first enemy gets killed and then I can kill the second enemy wich obviously is not what I want. The second one also only turns when the player is on the other side of the first enemy. That problem is probably the same mistake. I looked for multiple fixes but none of those have helped me so far. It would be great if someone could see the mistake I made. Here is EnemyShoot script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyShoot : MonoBehaviour
 {
     private Animator anim;
 
     public GameObject snowBall;
     public Transform throwPoint;
 
     public AudioSource throwSound;
 
     public float range;
     private float distance;
 
     private Transform player;
     private Transform enemy;
    
 
     private float timer;
     public float timeBetweenShots;
 
     // Use this for initialization
     void Start()
     {
         timer = 0;
         anim = GetComponent<Animator>();
         
     }
 
     private void Awake()
     {
         
         enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
         player = GameObject.FindGameObjectWithTag("Player1").transform;
 
        
     }
 
     private void Update()
     {
         timer += Time.deltaTime;
 
        if (enemy.position.x < player.position.x)
         {
             transform.localScale = new Vector3(1, 1, 1);
         } else
         {
             transform.localScale = new Vector3(-1, 1, 1);
         }
 
         distance = Vector2.Distance(player.transform.position, enemy.transform.position);
 
         if (distance < range && timer > timeBetweenShots)
         {
             
             GameObject ballClone = (GameObject)Instantiate(snowBall, throwPoint.position, throwPoint.rotation);
             ballClone.transform.localScale = transform.localScale;
 
             anim.SetTrigger("Throw");
 
             throwSound.Play();
 
             timer = 0;
         }
     }
 }



And here is the EnemyHealth script (there is an enemyCounterin the gamemanager script):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyHealth : MonoBehaviour {
 
 
     public int enemyLife;
 
     public AudioSource hurtSound;
 
     public void HurtEnemy()
     {
         enemyLife -= 1;
 
         hurtSound.Play();
         if (enemyLife == 0)
         {
             gameObject.SetActive(false);
             FindObjectOfType<GameManagerLevels>().enemyCounter -= 1;
         }
     }
 }


All help is much appreciated :)

Comment
Add comment · Show 2
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 Chikari · Feb 06, 2017 at 04:46 PM 0
Share

Can you show the code where you invoke HurtEnemy()?

avatar image VColson Chikari · Feb 06, 2017 at 04:56 PM 0
Share

Sure, I forgot to add it, this is in the script that's on the snowball (bullet) :)

  void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player1")
         {
           
             FindObjectOfType<Game$$anonymous$$anagerLevels>().HurtP1();
         }
 
         if (other.tag == "Enemy")
         {
             FindObjectOfType<EnemyHealth>().HurtEnemy();
         }
             Instantiate(snowBallEffect, transform.position, transform.rotation);
         Destroy(gameObject);
     }
 }
 

1 Reply

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

Answer by Chikari · Feb 06, 2017 at 05:03 PM

The problem is that you are using "FindObjectOfType" in script 2 and "FindGameObjectWithTag" in script 1. These functions return the first GameObject matching the description. In your case, always the first enemy.

In script 1, replace

 enemy = GameObject.FindGameObjectWithTag("Enemy").transform;

with

 enemy = this.transform;

I am assuming that script 1 is attached to the enemy object. So "this" will be the correct enemy. In script 2, replace

 FindObjectOfType<EnemyHealth>().HurtEnemy();

with

 other.gameObject.GetComponent<EnemyHealth>().HurtEnemy();

This will get the corresponding collider enemy. Then you should be good to go.

Comment
Add comment · Show 3 · 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 VColson · Feb 06, 2017 at 05:14 PM 0
Share

It's working ! Thank you so much :D

btw the enemies don't do anything when I start the level up but they do when I reload the level, do you happen to know why that is ? If not thanks anyways you helped me a lot already :)

avatar image Chikari VColson · Feb 06, 2017 at 05:23 PM 0
Share

I am guessing that your Awake() function in the enemy script is called before a player exists. Thus, the enemies do not detect the player.

If I were you, I'd get rid of the "Awake()" function completely. The execution order is not guaranteed. It may very likely cause a lot of hard to debug problems later.

$$anonymous$$aybe you should check if there is a player in the Update() function, and assign it (if it exists) to your "player" variable there.

avatar image VColson Chikari · Feb 06, 2017 at 09:02 PM 0
Share

I tried that but didn't work, thanks anyways :) It's not to big of a problem because when I build and run it the levels will be opened from a menu and then the problem isn't there.

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

328 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 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 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

How to make an enemy 2d chasing player? 1 Answer

Enemy AI ignores the distance from the player 0 Answers

Boss ai help EoW 0 Answers

Random Number keeps generating over and over 2 Answers

Corgi Engine - Character glitches with "jump" animation on top of a ladder 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