Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Inheritance Problem

Discussion in 'Scripting' started by HelloKity1231, Jul 14, 2014.

  1. HelloKity1231

    HelloKity1231

    Joined:
    Nov 25, 2012
    Posts:
    121
    Hello, i searched about inheritance in Unity and i found that Inheritance in Unity3D not work like .NET C#

    For Example:

    Code (CSharp):
    1.  
    2. public abstract class Spell
    3. {
    4.     string    name;
    5.     string    description;
    6.     float    cooldown;
    7.     float    castTime;
    8.  
    9.     public Spell(string name, string description, float cooldown, float castTime)
    10.     {
    11.         this.name = name;
    12.         this.description = description;
    13.         this.cooldown = cooldown;
    14.         this.castTime = castTime;
    15.     }
    16.  
    17.     public abstract void Fire();
    18.  
    19. }
    20.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ProjectileSpell : Spell
    5. {
    6.     public override void Fire ()
    7.     {
    8.         throw new System.NotImplementedException ();
    9.     }
    10. }
    11.  

    Code (CSharp):
    1.  
    2. public abstract class Spell
    3. {
    4.     string    name;
    5.     string    description;
    6.     float    cooldown;
    7.     float    castTime;
    8.  
    9.     public Spell(string name, string description, float cooldown, float castTime)
    10.     {
    11.         this.name = name;
    12.         this.description = description;
    13.         this.cooldown = cooldown;
    14.         this.castTime = castTime;
    15.     }
    16.  
    17.     public abstract void Fire();
    18.  
    19. }
    20.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ProjectileSpell : MonoBehaviour, Spell
    5. {
    6.     void Start()
    7.     {
    8.         //Code Here
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         //Code Here
    14.     }
    15.  
    16.     public override void Fire ()
    17.     {
    18.         //TODO
    19.         throw new System.NotImplementedException ();
    20.     }
    21. }
    22.  

    To make it work, Spell must extend ProjectileSpell (inverse way)
    I don't like inverse inheritance, i want to code like i know from .NET and i don't want to learn new things that will confuse me...

    Im doing something wrong? or it don't work like .NET ?

    Sorry for my bad english!
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Multiple inheritance is not supported nor C# (.NET) not in Unity.

    But good news is you can use aggregation or composition instead of inheritance. Or inherits your Spell class from MonoBehaviour.
     
    Last edited: Jul 14, 2014
    HelloKity1231 likes this.
  3. HelloKity1231

    HelloKity1231

    Joined:
    Nov 25, 2012
    Posts:
    121
    Damn, you right!... why i thought that C# support multiple inheritance :( ...
     
    Last edited: Jul 14, 2014
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    You're maybe thinking of interface. You can implement unlimited number of them.

    Or you can do MonoBehaviour -> Spell -> ProjectileSpell
     
    HelloKity1231 likes this.
  5. HelloKity1231

    HelloKity1231

    Joined:
    Nov 25, 2012
    Posts:
    121
    What is the best method to implement a spell system (and not only)? multiple inheritance using Interfaces or put all in one class?

    Programmatically, Interfaces(Multiple Inheritance) is helping us to seperate the problem in smaller pieces, thats why im asking for it!.

    I could use what Patico told me, but im looking to create a flexible solution for my problem!

    So, i will check Interfaces some time!
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Can you explain why you would wish for multiple inheritance instead of a deeper hierarchy?

    What would this give you;

    Code (csharp):
    1. public class ProjectileSpell : MonoBehaviour, Spell { }
    that this wouldn't?

    Code (csharp):
    1. public class ProjectileSpell : Spell { }
    2.  
    3. public class Spell : MonoBehaviour { }
     
  7. HelloKity1231

    HelloKity1231

    Joined:
    Nov 25, 2012
    Posts:
    121
    LightStriker,

    ... ty ...

    This worked! (ProjectileSpell inherit Spell which inherit MonoBehaviour)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public abstract class Spell : MonoBehaviour
    5. {
    6.     string name;
    7.     public Spell() {
    8.  
    9.     }
    10.     public Spell(string name){
    11.         this.name = name;
    12.     }
    13.     public abstract void Fire ();
    14. }
    15.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ProjectileSpell : Spell
    5. {
    6.     void Start()
    7.     {
    8.         Debug.Log ("ProjectileSpell::Start()");
    9.     }
    10.  
    11.     public override void Fire ()
    12.     {
    13.         throw new System.NotImplementedException ();
    14.     }
    15. }
    16.  



    Again soz for my bad english!
     
    Last edited: Jul 14, 2014