- Home /
The question is answered
Abstract class question
Hi, im using polymorphism fro all my hitboxes, so they are derived from a Hitbox base class, but i dont want the user to be able to create an instance of Hitbox class, so i tried making it abstract
but im still able to create an instance, why? does it have something to do with that Hitbox inherits from Monobehaviour? what im doing wrong?
(totally different class)
Answer by Bunny83 · Feb 19, 2016 at 01:59 AM
No, you can't create an instance of an abstract class. If you try to attach a script with an abstract class to a gameobject, the Unity editor will show a popup message with this text:
Can't add script behaviour AbstractBase. The script class can't be abstract!
If you try to add this class from code (with AddComponent) you will see the same message in the console.
You said:
im still able to create an instance
How did you create an instance of that class? From what you posted in your question you only only try to create a variable of your Hitbox class, but not an instance.
Maybe i don't fully understand your problem. If the class is abstract you can't create an isntance. Not even with reflection, it's impossible.
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
How to store data in script and attach it later? 0 Answers
How should I structure my unit class relationships? 1 Answer
Can I preserve the values in a parent class when I overwrite it with a child class? 1 Answer
Accessing interface in a derived class through the parent class 0 Answers