- Home /
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.
this sounds too complicated. who hit you should be clear in the method that is called inflicting the damage
I want to make variables ins$$anonymous$$d of hardcoding it but I guess you suggest to just decrease my hp by a specific value like:
hp = hp - 25;
Are you not using OnCollisionEnter? Then you'd know which hits the player and no need to look for all EnemyController:
void OnCollisionEnter(Collision col)
{
EnemyController enemy = col.gameObject.GetComponent<EnemyController>();
if(enemy == null) { return; }
int damage = enemy.GetDamage();
}
Then you can apply the damage to the player.
Thank you very much. Works perfectly. I'm quite new and I did not know how to get the EnemyController component from my enemies. I was using OnCollisionEnter() but as I said I didn't know how to access the script which is attached to multiple GameObjects. I would never think you can put "col" in this situation before
gameObject.GetComponent<EnemyController>();
Answer by Vega4Life · Dec 11, 2018 at 05:18 PM
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.
So you mean I have to create 3 new scripts without any code in them? I don't know what's polymorphism neither inheritance. I also guess I have to change something in this part of script
public class Hellephant : $$anonymous$$onoBehaviour
and make it like that:
public class Hellephant : EnemyController
am I right?
You are correct. Inheritance just means (in simplest terms), it gets the class members and methods of its derived class - in this case EnemyController. It's like a copy paste. In other terms, it's like a dog is a mammal. $$anonymous$$ammals has all kinds of attributes - eyes, ears, legs. Because a dog is a mammal, the dog will have those same attributes a mammal has.
Polymorphism means that Hellephant is an enemy (because it derives from it). So anytime code is looking for an Enemy, you can pass Hellephant as the data type. The reverse on the other hand, isn't true. An Enemy isn't always a hellephant. So if code is looking for a hellephant, you can't just pass an enemy to it.
Your answer
Follow this Question
Related Questions
GUITexture multiple component activation 1 Answer
OnTriggerEnter Script only working on duplicated object 1 Answer
How to load multiple entries from one Binary file? 1 Answer
How to access other scripts from one script 0 Answers
Custom Editor Access to Scene Objects Variables (in the same way as UnityEvents) 1 Answer