I'm creating a game where I have an Entity class that contains basic entity info (pos, health,..) and some functions. Enemies and Player later on inherit from this class.
Public class Entity {
public Vector2 position;
public float health;
,...
Lets say that I have a function like this inside the Entity class:
public GameObject movingIntoEnemy(Vector2 movingToPoint)
{
foreach (GameObject go in GameObject.Find("GameMap").GetComponent<GameMap>().enemyList)
{
if (go.GetComponent<EnemyScript>().enemy.position == movingToPoint)
{
return go;
}
}
return null;
}
This function looks for a GameObject called "GameMap" and then cycles through enemies in it'sits list. If an enemy is found, it returns it'sits GameObject. Using GameObject.Find() is pretty ineffective, so what would be a better approach to this? I mean, I can't just easily use
public GameObject GameMap;
since the code is in a class, and this class needs to be instantiated first in my player/enemy scripts.
My question is - how do I attach gameObjectsGameObjects to this class before it is even created? What is the appropriate approach to this? I guess that I might pass these GameObjects to the class through it'sits constructor, but it would make things much more complicated.
Thanks in advance ;)