- Home /
Multi inheritance in Boo
Hey Guys, is there any way or alternative to inherit from multible classes? Basically I want to have to classes, a player class and a character class, since a character can be also another player I want to inherit the player class from the character class. Here's my Code: Character.boo
import UnityEngine
public class Character (MonoBehaviour):
health as single = 100
maxHealth as single = 100
stamina as single = 100
maxStamina as single = 100
def kill():
pass
def respawn():
health = 100
stamina = 100
Player.boo
import UnityEngine
public static class Player (MonoBehaviour, Character):
def Update():
pass
The current error is "Assets/Scripts/Player/Player.boo(3,44): BCE0001: The class 'Player' already has 'UnityEngine.MonoBehaviour' as its super class. "
Thanks in advance!
Answer by Bunny83 · Oct 25, 2014 at 09:50 PM
If you derive your Player from the Character class the Player will also be a MonoBehaviour since the Character class os derived from MonoBevhaviour. So just derive your player from Character and you'll be fine.
.NET / Mono only allow one base class per class, there's no way around that.
Keep in mind that MonoBehaviour is also derived from Behaviour and Behaviour from Component and Component from UnityEngine.Object. So your complete class chain would look like this:
Player -> Character -> MonoBehaviour -> Behaviour -> Component -> UnityEngine.Object -> System.Object
edit
I just reread your title. You said you want to use multi inheritance but what you actually need is simple inheritance, at least what we can understand from your description.
PS: $$anonymous$$eep in $$anonymous$$d that every Player instance is also a Characer as well as a $$anonymous$$onoBehaviour and a Component.
However not every Character has to be a Player. You could create another class called NPC which is also derived from Character. So an NPC and a Player are both a Character but NPC and Player are not interchangable.
Answer by Kiwasi · Oct 25, 2014 at 09:43 PM
No. This is not possible.
There are two plausible solutions. Your class can inherit from player, player can inherit from monobehaviour. Or you class can implement an interface.