- Home /
One script 2+ Enemies simple one
Hi i wanted to make a script that works on all enemies when the player hit one of them it was a lot harder than i though i want to reach at least the currentHealth on each enemy without making tons of unwanted lines i ended up in a simple dummy script but i tried
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public bool animation1Allowed;
public p2Animation Player2;
public int number;
public GameObject enemy1;
public GameObject enemy2;
void start()
{
}
public void OnTriggerEnter(Collider collisionInfo) {
if (collisionInfo.gameObject.CompareTag ("Enemy"))
{
if (Player2.animationAttackFrame == true) {
animation1Allowed = false;
Player2.animationAttackFrame = false;
number = 1;
damage(5);
}
}
if (collisionInfo.gameObject.CompareTag ("Enemy1"))
{
if (Player2.animationAttackFrame == true) {
animation1Allowed = false;
Player2.animationAttackFrame = false;
number = 1;
damage(5);
}
}
}
public void damage(int DOP)
{
enemy1.GetComponent<CEnemy1Ai> ().currentHealth -= DOP;
}
public void damage1(int DOP)
{
enemy2.GetComponent<CEnemy2Ai> ().currentHealth -= DOP;
}
}
Answer by Andres-Fernandez · Jul 14, 2014 at 06:38 AM
If you are looking for a way to make all the enemies to substract some health points when something occurs (the player collides with one of them) you have a couple options.
The first one is to have a reference to all enemy objects inside an array and use that array to call your damage function. Something like:
public GameObject enemies[];
public void OnTriggerEnter(Collider collisionInfo) {
if (collisionInfo.gameObject.CompareTag ("Enemy"))
{
for (int i = 0; i < enemies.Length; i++) {
damage(enemies[i], DOP);
}
}
}
And then your damage function looks like this:
public void damage(GameObject obj, int DOP)
{
obj.GetComponent<CEnemy1Ai> ().currentHealth -= DOP;
}
The downside of this is that you need to register all enemies whenever you create or destroy an enemy.
There is another way a bit more elegant and actually better, as it doesn't require you to manage an enemies array, and is using events. You can have the player send an event whenever it collides with an enemy and all enemies handle that event within their own code (and substract the DOP). I recommend you to check the Event system tutorials.
I still don't understand how could
if (collisionInfo.gameObject.CompareTag ("Enemy")) work for 2 enemies i tried to understand but it still will work whenever i hit the tagged enemy enemy and he is only 1 all i want is 1 script attached to 2 enemies and they work completely separate when i hit one of them by playerAttack script
Wait, I got the question completely wrong. What you meant is that you want to use just one function to make any enemy substract some health points? I thought you wanted it to make all the enemies to substract some hp.
If that's the case, then my answer is wrong, as it calls damage function for every enemy. What makes my answer wrong is the for loop that calls damage function for all enemies! (the CompareTag("Enemy") detects just one enemy, but the for loop triggers all the enemies).
To call damage function only in the enemy you collided with, I would place the damage function inside the enemy script (the one attached to the enemy) and inside your triggerEnter function call Send$$anonymous$$essage. It would look like this:
public void OnTriggerEnter(Collider collisionInfo) {
if (collisionInfo.gameObject.layer == Layer$$anonymous$$ask.NameToLayer ("Enemy"))
{ collisionInfo.gameObject.Send$$anonymous$$essage("damage", DOP, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
You just check if you collided with an object in the "Enemy" layer (which is the one your enemies will be in) and tell it to call function damage with DOP. Then, add the damage function in your CEnemyAI script:
public void damage(int DOP) {
currentHealth -= DOP;
// rest of stuff
}
This way, your PlayerAttack doesn't need a function for each enemy. It just tell whatever it collides with to call its own function damage.
Your answer
Follow this Question
Related Questions
Text displays during animation 1 Answer
Cleanest way to decouple Manager class with events 1 Answer
button to select what player shoots 0 Answers
switching scripts on and off 1 Answer
Event.current.Use makes GUI disapear ONLY in standalone 2 Answers