Hello, i’m looking for a solution to this problem i’m having. I’m trying to get an object’s component which inherits from a base class, but when I do I get this error:
Any way to fix this? Thank you in advance.
Hello, i’m looking for a solution to this problem i’m having. I’m trying to get an object’s component which inherits from a base class, but when I do I get this error:
Any way to fix this? Thank you in advance.
Like the error says, GetComponent only works with MonoBehaviour or Component derived objects. Or with interfaces.
Does the base class inherit from MonoBehaviour or Component?
If not, you could always do a workaround by creating a dummy interface for Base_Ally so that it’s findable by GetComponent. It’s silly, I know, but there it is.
Sorry, I don’t really get this! How is GetComponent supposed to find it after I make an interface?
class BaseAlly : MonoBehaviour {}
class SuperCoolAlly : BaseAlly {}
Oh wait, now I get it. Thank you very much, guys.
Wait nvm. I forgot that my Base_Ally class derives from another one. That other class, Base_Class, does not derive from Monobehaviour(it’s indipendent), which makes it impossible to be grabbed by GetComponent. What do I do?
In that case it is no longer a Component, so you would need some other method of storing the link between Transform objects and “Base_Class”. Unity is built to work with components if you intend to associate them with GameObjects, is there a reason it can’t inherit from MonoBehaviour, or even just Component? If not, you’ll need to have a property on the Base_Class referencing the GameObject, or a static dictionary or something along those lines.
I wanted to avoid using too many statics but I guess I have no choice. Thank you for the help!
Yeah it’s a necessary evil… if you create your own objects, there’s no way Unity is going to know about them. You’ll have to track them yourself.