W
W
WOSNE2021-11-06 17:12:11
C++ / C#
WOSNE, 2021-11-06 17:12:11

Is it possible to set a condition in the name of GetComponent?

Hello everyone) Help)
For example, there is СSelectedSpell.gameObject.GetComponent<Fire>.CastSpell()
Here the scheme is standard, everything works)
But is it possible to do something differently, for example:
СSelectedSpell.gameObject.GetComponent<СSelectedSpell.name of the Object>.CastSpell ()
// it does not allow to set the condition in this way

In general, it is necessary that it does not read the name of the component directly, but takes, for example, the name of the object and substitutes this value in the name of the component.
And instead of not writing dozens of options, I want to make one simple line of code.

How exactly can you set the conditions for the name of the component and is it possible to do this at all?
GetComponent <So that different values ​​\u200b\u200bare substituted here> :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2021-11-08
@WOSNE

I correctly understood that you have a lot of different spells, but the script from which you are trying to "cast" the spell cannot know which class it belongs to?
Create a Spell class with a CastSpell method. Inherit from Spell already, say, Fireball and the rest, redefine the method in them. Then you can use GetComponent < Spell>() even if it's a Fireball. Downcast is happening if I'm not mistaken. This is OOP.
Or you can try to solve with the help of events. Guess I'd try this if the active spell keeps changing.

Y
Yura Milevsky, 2021-11-07
@Hackerman1

Either shove these values ​​into variables, and then shove these variables into <>, or not at all! You can not do it this way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question