- Home /
Question by
Sitgreaff · May 26, 2020 at 04:19 PM ·
c#collision2d gamecollider2dontriggerenter2d
Alternative OnTriggerEnter2D for class instances
There is a class “Enemies” containing a method that needs to be called when a instance of the “Enemy” collides with a game character. Instances differ only in characteristics (class fields). In this case, I create a separate script for each type of enemy, in which I write about the same thing, although in fact only the characteristics need to be changed. After that I bind the script to the prefab of the desired opponent. Is it possible to transfer to OnTriggerEnter2D, for example, a public List ? Or in what other way can I implement the creation of all instances of the class in one script, without tying it to prefabs?
class Enemy
{
// public static int "lot of a characteristics"
public Enemy( //lot of a characteristics )
{
// this.lot of a characteristics = lot of a characteristics ;
}
public void onCollision(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
//code
AfterEnemyDeath();
pushEnemy();
}
}
private void AfterEnemyDeath()
{
//code
}
void pushEnemy()
{
//code
}
}
//first enemy
public class Enemy_1: MonoBehaviour
{
public GameObject enemy_1_obj;
Enemy enemy_1;
private void Start()
{
enemy_1 = new Enemy(lot of a characteristics );
}
private void OnTriggerEnter2D(Collider2D collision)
{
enemy_1.onCollision(collision);
}
private void Update()
{
if (enemy_1.live == false) Destroy(enemy_1_obj);
}
}
//second enemy
public class Enemy_2: MonoBehaviour
{
public GameObject enemy_2_obj;
Enemy enemy_2;
private void Start()
{
enemy_2 = new Enemy(lot of a characteristics);
}
private void OnTriggerEnter2D(Collider2D collision)
{
enemy_2.onCollision(collision);
}
private void Update()
{
if (enemy_2.live == false) Destroy(enemy_2_obj);
}
}
Comment