- Home /
Question by
guitargodd97 · Oct 01, 2012 at 01:16 PM ·
arraysenemieshealth
Arrays of Enemies in a Game HELP!
I am using the following script to set up any enemy/player/whatever in game with simple attributes like attack, defense and health.
private int _attack, _defense, _maxHealth, curHealth;
private Transform _myTransform;
// Use this for initialization
void Start () {
_myTransform = transform;
if(_myTransform.tag == "Player") {
}
else {
if(_myTransform.gameObject.name == "Spider") {
_attack = 1;
_defense = 1;
_maxHealth = 1 * _defense;
}
else {
Debug.LogError("No Name Found! ERROR!");
}
}
}
Then I am using a script on the player to create a list of the GameObjects tagged "Enemy" in a simple line of code:
enemies = GameObject.FindGameObjectsWithTag("Enemy");
I declared the GameObject array above. My question is how can I set this up so that a player can list the closest GameObject from the enemies array and list that as targeted enemy. Then, how do I get it so that a player can press a button and interact with that enemies health variable based on that player's attack variable minus that enemies defense variable. I appreciate all the help I can get.
Thanks
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613081724im_/https://answers.unity.com/themes/thub/images/avi.jpg)