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 :)
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);
}
}
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.
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 :)
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.
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.