I have a tower that has a bullet prefab attached to it. The bullet has a script which deals damage to 1 type of enemy.using ontriggerenter. I want to be able to have multiple types of enemies but i can’t have more than 1 ontriggerenter on my bullet script. How could i do this so my bullet can attack more than 1 type of enemy?
Your OnTriggerEnter() is called every time your bullet trigger collider enters another collider, not only the one with a component called MonsterHealth.
Currently anytime your bullet hits any object it will attempt to find a MonsterHealth component, which will return a Null if it doesn’t exist. If you want to check what was collided with then use an if statement in the OnTriggerEnter method. Something like this:
void OnTriggerEnter(Collider co) {
if(co.gameObject.name == "monster") {
//Reduce its health
}
else if(co.gameObject.name == "wereWolf") {
//Do something else
}
}
BETTER
By the way, it would be better to have a generic Health script on any type of enemy that can take damage, which just stores the enemy health and can reduce it. In the OnTriggerEnter method just find this script and it will be able to damage anything you give that script to, without using a clumsy if/else statement as above.
Example:
Create a script called Health as below:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
int health;
public void Damage() {
health -= 1;
if(health <=0) {
Die();
}
}
void Die() {
//Do anything you need to do during death
//then:
Destroy(gameObject);
}
}
then attach this to all of your objects you want to take damage.
In your bullet script:
void OnTriggerEnter(Collider co) {
Health health;
if(health = co.GetComponent<Health>()) {
//Reduce its health
health.Damage();
}
}
EDIT: Sorry, I read the comments before I understood your question:
You need to make it so all your enemies share a common ‘enemy’ component. then have every enemy inherit that component. Now when your bullet hits things, it doesn’t have to worry about what kind of enemy it is, it just hits anything that is an enemy.
public class Enemy : MonoBehavior
{
//has health
}
public class Werewolf : Enemy
{
//does Werewolf stuff
}
public class Vampire : Enemy
{
//does Vampire stuff
}
There are probably better ways to do this, and I’m looking forward to find out what they are, but in the mean time this is what I usually do. Just an example:
using UnityEngine;
class EnemyType
{
}
class EnemyType1:EnemyType
{
}
class EnemyType2:EnemyType
{
}
public class Bullet : MonoBehaviour
{
void DamageEnemy (EnemyType1 enemy)
{
//blah
}
void DamageEnemy (EnemyType2 enemy)
{
//blah
}
void OnTriggerEnter (Collider coll)
{
var other = coll.gameObject;
EnemyType1 enemyType1 = other.GetComponent<EnemyType1> ();
if (enemyType1 != null) {
DamageEnemy (enemyType1);
}
EnemyType2 enemyType2 = other.GetComponent<EnemyType2> ();
if (enemyType2 != null) {
DamageEnemy (enemyType2);
}
}
}
Here you can give tag from inspector to each enemy and in OnTriggerEnter method filter triggered object tag wise
For example…
If you have given tag “enemy1” to type 1 enemy and “enemy2” to type 2 enemy then your code of OnTriggerEnter can be like
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag== "enemy1") {
//Code goes here
}
else if(other.gameObject.tag== "enemy2") {
//Code goes here
}
}
