Passing enemy statistic to FightController
I created a fighting system between player and enemy with easy customization so I can just copy/paste the old enemy and change some values like speed and health to create completely new enemy. I build this without knowing I can't have a script with the same name in many different folders. My system work so that when collision between player and enemy is detected, InitFight()-function is called (in my FightHandler). This InitFight()-function searches the enemy gameObject collided with for a script called "Stats" and gets enemy variables from there like this:
enemyDefence = GV.NPCUnderAttack.GetComponent<Stats>().defence;
I spent nearly 30 hours making my FightHandler what I think is perfect for my needs but when I started creating my first new enemy I got this error: "The namespace `global::' already contains a definition for" which means I have two scripts with the same name in different locations. To make it more understandable look at demonstration of my project hierarchy (also the Chicken folder has ChickhenColor, ScoutAround and Stats):
I've tons of code in my Fighting system and it all relies enemies having a script called Stats otherwise nothing will happen. I clearly wouldn't like the start planning everything from the beginning so does anybody got good ideas? Could I use something like:
void OnCollisionTrigger(Collider col){
string nameOfNPC = col.gameObject.name;
string scriptToSearchFor = nameOfNPC + "Stats";
enemyDefence = GV.NPCUnderAttack.GetComponent<scriptToSearchFor>().defence;
}
And then just name each of the enemies Stats.cs file like: CowStats.cs, ChichkenStats.cs, ReindeerStats.cs. Would that be a good way? If it's possible how would it affect my performance?
Thanks in advance
You have multiple scripts named Stats? Why not have one script that gets used by all enemies? Then it can stay simple:
void OnCollisionTrigger(Collider col){
enemyDefence = col.gameObject.GetComponent<Stats>().defence;
}
Your answer
Follow this Question
Related Questions
How to disable a script in another without Public GameObject? 0 Answers
Difficulty setting script on gameObject from another script. 0 Answers
What to use instead of GameObject.Find and GetComponent 2 Answers
gameObject.SetActive (false); not working 2 Answers
Hi everyone.need help with this. 1 Answer