- Home /
How can i get a unknow GetComponent??
If there are too many characters in the scene, they are also a characters inheritance from charater_base script and they are different name of script for different skill (Lily script , Peter script , Join script .......) But the bullet script can only GetComponent with a single script to active the function "Take_Damage" in the character_base script. If all the character is tag with "chara" . Is there are some anyway for solve this problem?
thankyou for your reply
Whaaaaaaat….? Is there a problem located somewhere in there? I have absolutely no idea what you are asking.
$$anonymous$$y apologies if it is a language barrier that is making this hard for me to understand, but take your time and compose yourself, get a proper translation online, and try again before you just type a stream of conscious.
I am sorry for my poor english...
The question is simple...Before we GetComponent with the other gameobect... It need the know the name of script $$anonymous$$G. other.GetComponent ().TakeDamage (10f);
But in my case, I want to add the function for the bullet other.GetComponent ().TakeDamage (10f)
In the ????, It maybe different type of character. $$anonymous$$aybe they are saber, caster , archer...in the game. It need to write all possible type of character for complete the script. But I think it is not a good method.
Answer by Bunny83 · Jul 23, 2019 at 06:21 PM
I don't quite get the issue. If you have all those script derived from your "charater_base" class and the TakeDamage method is declared in that base class, why don't you just do
var character = GetComponent<charater_base>();
if (character != null)
character.TakeDamage(10);
That's the whole point of having inheritance. So you can treat derived classes like a base class instance since it actually is a "charater_base" instance as well.