Simple question about single or multiple Monobehaviours
I am creating a platformer game and the game contains 5 different characters, and each character have certain amount of health, movement speed and different abilities.
Character 1 have the ability to shoot proyectiles, while Character 2 have the ability to do a dodge movement, which increases movement speed and dodges everything that damages him.
I am currently using a single Monobehaviour called "PlayerControl" which makes the character to jump, move and use abilities.
I have a class named "Character" that contains every variable that the character will have and 3 methods , public void ability1(), ability2(), etc...
In PlayerControl I have a variable: public Character character; and it gets initialized by other script and works well.
This works well, but the problem is that when I want a character to spawn something, like a proyectile(ie character 1 spawns an arrow and character 3 spawns a ball) I have to create a function in PlayerControl to make this happend and maybe for every ability that needs to spawn something, because class "Character" and it's inherited members aren't monobehaviours and I cant use "Instantiate" in Character1's ability1();
The question is: Making a single monobehaviour for each character is better than 1 for all characters because of things mentioned before? And if the answer is NO, How can I make that single "PlayerControl" acts like a global controller for every character without having, for example, a function "public void spawnObject(GameObject object,bla,bla,bla){}" where blablabla may be velocity, or some other values that some proyecitles need in order to work?
I know this is a basic stuff, but I am kinda confused.
Thanks for reading!
Answer by Skyking · Apr 17, 2017 at 08:52 AM
What you are looking for is inheritance.
Unity has a great tutorial about it, enjoy! https://unity3d.com/learn/tutorials/topics/scripting/inheritance
Your answer
Follow this Question
Related Questions
Help with a roll effect 2D 0 Answers
Get and Set only work on public variable? 1 Answer
Having problems with Overlap Circle 0 Answers
Setting up a New Data Type - Am I Doing Something Wrong? 1 Answer