In Unity, the way to execute a method from another script is as follows::
Objeto.GetComponent<NomeDaClasseDoScript>().NomeDoMetodo();
Or
Objeto.GetComponent("NomeDaClasseDoScript").NomeDoMetodo();
The second form has disadvantages in performance (because it needs to reflect in a sense similar to what @Maniero has described to you), but it works for what you want. So you can do it this way:
Create different scripts instead of different methods in a single script. Each script will have the name you are setting (in transform.name
). And all of them will have the same method animation
(of course, each doing what they have to do differently).
Execute the method as follows: Enemy.GetComponent(Enemy.transform.name).Animation();
. Note that this can cause errors while running your game if the script with the name Enemy.transform.name
not exist!
That said, I would like to mention that I understand what you want to do but I don’t understand exactly why you want to do this. You are centralizing control of the animation in a class called AnimationManager
, when it could have left it to the class of each enemy (the enemy can also detect the collision and, alone, perform its animation). From the point of view of object orientation this is a very bad choice, because it creates an unnecessary coupling and will certainly hinder its future maintenance.
What is this
Enemy.transform.name
?– Maniero
Enemy.transform.name returns a string with the name of the game object.
– Filipe Moraes
Ta with all the face that should have an interface in place of this concrete implementation of object.
– Intruso