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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Inheritance problem:

Discussion in 'Scripting' started by Superkorlas, Dec 12, 2015.

  1. Superkorlas

    Superkorlas

    Joined:
    Jul 30, 2015
    Posts:
    9
    Hi everyone! I have a problem with inheritance.
    I'm following a book (to learn more :rolleyes:) and when I do what the book tell me I have a problem.
    I have a class entity like this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Entity : ScriptableObject
    4. {
    5.     public string Name;
    6.     public int Age;
    7.     string Faction;
    8.     public string Occupation;
    9.     public int Level = 1;
    10.     public int Health = 2;
    11.     public int Strenght = 1;
    12.     public int Magic = 0;
    13.     public int Defense = 0;
    14.     public int Speed = 1;
    15.     public int Damage = 1;
    16.     public int Armor = 0;
    17.     public int NoOfAttacks = 1;
    18.     public string Weapon;
    19.     public Vector2 Position;
    20. }
    Here all fine. And now I must create a class like the next and attach it to a NPC-character (game object):
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Npc : Entity
    4. {
    5.  
    6. }
    Note: yes, it is empty class but in the future I add some code o_O
    But unity doesn't permit me attach it to a game object because it tell me the class must inherit of MonoBehaviour's class.
    What I must do?
    If I don't attach it I can't continue with the book so I need help. I try use multiple inheritance like this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Npc : Entity, MonoBehaviour
    4. {
    5.  
    6. }
    But it doesn't compile.
    Note: the book is based on Unity 4.3 and I'm using Unity 5.2.3f1
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    http://docs.unity3d.com/ScriptReference/ScriptableObject.html

    "A class you can derive from if you want to create objects that don't need to be attached to game objects."

    Are you sure you want to extend this way? ScriptableObject -> Entity -> Npc?

    https://msdn.microsoft.com/en-us/library/ms173156.aspx

    "By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes."

    So either the book is doing something weird, or maybe you want to Entity to extend MonoBehaviour instead. Although Entity seems to be mostly holding data and no behaviour, so maybe you want to just have it as a property/"component" on your Npc script instead of extending it?
     
    Superkorlas likes this.
  3. Superkorlas

    Superkorlas

    Joined:
    Jul 30, 2015
    Posts:
    9
    First, I want say thanks a lot for your faster answer.
    I suppuse is a errata's book.
    I will read the interfaces. I will do what you tell me, use the entity like a component, I suppuse it is what the book wanted say.
    Again, thanks a lot.