- Home /
What are the conventions for overloading classes?
I have a project where I need to have several different variations of one class, and my system works fine except that I can't apply these overloaded classes to any objects.
.
My project works like this: I have a 'buildingBlock' class, which has functions to be placed and deleted. I want to have a block that has more functions, for example a door that can be opened and closed. For this, I have made a class 'doorBlock' that inherits from 'buildingBlock' and this works fine in code, but because I made this overload class inside the 'buildingBlock' script I can't apply it to any objects.
.
My question is whether I should have the 'doorBlock' class in another script, or if there's some way to choose between classes inside of a script from the editor. Doing it either way isn't really a problem, it just seems like it would be a mess to have a bajillion different scripts that are all slightly different.
Answer by daotanp · Jan 02 at 06:46 AM
From what I understand, you put both buildingBlock class (which is a MonoBehaviour) and doorBlock class (which inherits from buildingBlock) in a single file, and you want to attach the doorBlock to a GameObject.
Unfortunately there can not be more than one MonoBehaviour in a file. Since buildingBlock is a MonoBehaviour, doorBlock is a MonoBehaviour too. You will need to put the doorBlock class in new file in order to apply it to any GameObject.
This.
In addition it is just good style to create new files for new classes. Since a building system might have like unlimited other parts you do not want to have all of those buildingBlock derivatives in one file.
Thanks, this was one of those questions that you can't bluff your way through.
Your answer

Follow this Question
Related Questions
How can I get the name of a class into a script from the editor. 1 Answer
Color set to black even though it's set by the editor 0 Answers
How do you make a custom inspector for a class or instance? 3 Answers
Access a class from Editor script? (Visual Studio) 1 Answer
Custom classes deleted on build/debug 2 Answers