Accessing ONE script attached to multiple GameObjects.

So I have 3 variables that I want to access and they are in one script. I could make it from another script but I am really curious how to make it and the code wold be clearer and more sensible where I want it to be. I tried to do it like that:

EnemyController[] enemyController;
enemyController = GameObject.FindGameObjectsOfType<>EnemyController>();

but then when I want to access these variables I can put only one number in these brackets: .
If you are curious what I’m doing and maybe it can make it clearer what I mean, then I want to decrease my player’s HP each time the enemy hits him. I have 3 enemies and 3 variables: hellephantDmg, bearDmg, bunnyDmg. The scripts EnemyController is attached to all three of them. I want to make if statement and detect which enemy player hits that’s why I want to access these 3 variables but the problem is I don’t know how… Please help.

I think this is a good spot to learn some inheritance/polymorphism.


You have an EnemyController and it only needs to have a single variable of ‘hitpoints’ in it. Then, you create three other scripts called “Hellephant”, “Bear”, “Bunny”. BUT, all three of these scripts derive from EnemyScript. Thus, they get access to their own ‘hitpoints’ (just make it protected), AND they are considered an EnemyController.


This way, you can store all your enemies as an EnemyController and get a single point of access to ‘hitpiont’. If you need to know if if an EnemyController is a specific enemy, you do if (EnemyController is Hellaphant) { “do things specifically to hellaphant”};


I went through it pretty fast, but it should get the idea across.