I am building a 2D turn-based RPG battle scene with Unity 5. The enemies are represented as 2D sprites with box colliders. In my script, the relevant enemy class is defined as:
public class EnemyView : MonoBehaviour {
public SpriteRenderer _renderer;
public BoxCollider2D _collider;
void Awake() {
_renderer = gameObject.GetComponent<SpriteRenderer>();
_collider = gameObject.GetComponent<BoxCollider2D> ();
}
}
And I have the following code grouping the enemy sprites together:
public class MainGUIView : MonoBehaviour {
public EnemyView[] _enemyViews;
}
When a player clicks on an enemy, I would like to know the index of that enemy within the _enemyViews
array. I thought of implementing this within EnemyView
class but that class has no information about its index, so I am thinking of implementing OnMouseDown()
within MainGUIView
where I could iterate through _enemyViews
to find the one whose collider contains the mouse click point, but I don't know how to write the code. Anyone can point me to some useful code snippet?