Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
37
Question by Ehren · Oct 23, 2009 at 06:00 PM · scriptingbasicsinheritancesubclassingoafa

Does Unity support Inheritance and Subclassing?

If I have a few different characters/objects in my game that all share some common behavior, can I use inheritance to centralize the common behavior into a base class? Or do you recommend another approach?

To give a simple example, I might want to have a character controlled by the player, and other characters with similar behavior that are controlled by an AI routine.

If inheritance is possible, do the base class and subclasses go in separate files? Do I only have to attach the relevant subclass file to a particular game object, or the base class file as well?

Finally, is inheritance possible in both C# and UnityScript?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image newbprofi2 · May 12, 2013 at 10:37 AM 2
Share

Important note (Thats the only reason why author asked this question, and no one provided the right answer):

Unity supports inheritance, BUT, with 1 condition: Unity "Allocates" only that class, which have the same "Name" as "FileName". So if you want to allocate specific object from script in Inpector, you must name script with specific object's name.

INVALID example:

 //
 // $$anonymous$$yBehaviour.cs
 //

 public class $$anonymous$$yBehaviour : $$anonymous$$onoBehaviour
 {
   public virtual voud Start()
   {
     Debug.Log("$$anonymous$$yBehaviour");
   }
 }
 
 public class $$anonymous$$yDerivedBehaviour : $$anonymous$$yBehaviour
 {
   public override voud Start()
   {
     base.Start();
     Debug.Log("$$anonymous$$yDerivedBehaviour");
   }
 }
 
 // You will see only: "$$anonymous$$yBehaviour",
 // because unity Allocated only class,
 // with the same name as "$$anonymous$$yBehaviour.cs".



VALID example:

 //
 // $$anonymous$$yDerivedBehaviour.cs
 //
 
 public class $$anonymous$$yBehaviour : $$anonymous$$onoBehaviour
 {
   public virtual voud Start()
   {
     Debug.Log("$$anonymous$$yBehaviour");
   }
 }
 
 public class $$anonymous$$yDerivedBehaviour : $$anonymous$$yBehaviour
 {
   public override voud Start()
   {
     base.Start();
     Debug.Log("$$anonymous$$yDerivedBehaviour");
   }
 }
 
 // Now Unity Allocated $$anonymous$$yDerivedBehaviour,
 // we will see "$$anonymous$$yBehaviour" and "$$anonymous$$yDerivedBehaviour".
avatar image kubarium · Nov 16, 2013 at 09:35 PM 1
Share

INVALID And VALID look exactly the same except comment lines. This is most confusing.

avatar image Nition · Nov 16, 2013 at 09:53 PM 0
Share

Different filenames. He's pointing out that $$anonymous$$onoBehaviour classes have to have the same name as their filename. It's not quite the whole answer though - see the further discussion on the accepted answer.

avatar image The_Magical_Kiwi · Mar 31, 2014 at 04:13 PM 0
Share

so if I'm understanding right, in order to be able to assign a particular class to an object you need to make a separate script for each one, there is no way to have more than one class which derives from monobehaviour in the same script?

avatar image Draco18s · Mar 31, 2014 at 10:34 PM 0
Share

Right, because $$anonymous$$onoBehaviour is a CLASS level inheritance object and you can only extend one class. You can, however, create Interfaces and implement as many of those as you'd like:

 class $$anonymous$$yThing : $$anonymous$$ySuperThing,I$$anonymous$$oney,IInventoryItem {...}

9 Replies

· Add your reply
  • Sort: 
avatar image
29
Best Answer

Answer by flaminghairball · Oct 23, 2009 at 08:28 PM

Subclassing (inheritance) is available in the Unity flavor of JavaScript as well as in C#. See MSDN for how to use inheritance in C#. To have an inheritance chain in JavaScript:

  • Create a new JavaScript
  • Put in this code:

    class MyCoolObject extends MonoBehaviour {
        var myCoolInt : int;
        var myCoolFloat : float
    }
    

    Now you have a new class that implements all the methods of MonoBehaviour(such as Update() and FixedUpdate()). If you want to create a subclass of MyCoolObject, just:

    class MyCoolerObject extends MyCoolObject {
        var myCoolerInt : int;
        var myCoolerFloat : float;
    }
    
  • Now if you reference MyCoolerObject in a script, both the cooler variables and the cool variables will show up in the inspector.

  • Comment
    Add comment · Show 7 · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image Ricardo · Oct 30, 2009 at 09:02 PM 0
    Share

    Fla$$anonymous$$gHairball answers the question quite well - since $$anonymous$$yCoolerObject descends from $$anonymous$$yCoolObject, it already contains all its members - you only need to attach the descendant and you will get the parent's properties and methods.

    avatar image AngryAnt ♦♦ · Nov 05, 2009 at 03:26 PM 1
    Share

    C# does support inheritance in the same way as well, yes.

    avatar image MountDoomTeam · Dec 30, 2012 at 11:40 AM 0
    Share

    about part .4 oh the above answer-

    How do you reference $$anonymous$$yCoolerObject class in a script?

    avatar image Golan2781 · Dec 30, 2012 at 12:34 PM 0
    Share

    Seeing how these are derived from $$anonymous$$onobehavior, you'd use them just like every other component - add them to an object in your scene. You can then reference them from other components, on events (like a collision) or if another object uses a method to interface with this object.

    The generic GetComponent.() function would be a good start if you want to get into using custom modules.

    avatar image Chronos-L · May 12, 2013 at 08:21 AM 1
    Share

    @newbprofi2, would you kindly explain your opinion?

    Show more comments
    avatar image
    20

    Answer by Peter Alexander · Dec 28, 2009 at 05:50 AM

    Unity does support subclassing, but it does NOT support dynamic dispatch for "Unity" method calls.

    For example:

    public class MyBehaviour : MonoBehaviour { private void Start() { Debug.Log("MyBehaviour created"); } }

    public class DerivedBehaviour : MyBehaviour {

    }

    If you attach a DerivedBehaviour script to a GameObject, it will NOT call MyBehaviour.Start() on startup. If you want to do that then you have to do it manually:

    public class MyBehaviour : MonoBehaviour { private virtual void Start() { Debug.Log("MyBehaviour created"); } }

    public class DerivedBehaviour : MyBehaviour { private virtual void Start() { base.Start(); } }

    Note that making the methods virtual in the first example would not fix the problem.

    This applies to all "Unity" calls (e.g. Awake, Start, Update, LateUpdate, OnLevelLoaded etc.). "Normal" method calls work correctly like you would expect in any .NET app.

    Comment
    Add comment · Show 12 · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image AngryAnt ♦♦ · Jan 04, 2010 at 01:55 PM 3
    Share

    The reason why you're seeing this is that Awake, Start etc. are not actually methods on the $$anonymous$$onoBehaviour class, but rather messages sent to derivatives of it. You'll therefore see the same behaviour from Send$$anonymous$$essage use.

    avatar image Bunny83 · May 03, 2011 at 03:12 PM 9
    Share

    That's not true. You have it just a little bit wrong. You should get a CS0621 because virtual methods can't be private! It wouldn't make much sense either. Furthermore you have to override the method in the derived class, otherwise you reimplement it and you would hide the base method. I did what you did but use protected for both methods and override in the derived class and it works as expected.

    avatar image sacredgeometry · Aug 09, 2011 at 05:22 PM 0
    Share

    Thank you so much.

    avatar image steinbitglis · Sep 27, 2011 at 11:04 AM 0
    Share

    I don't have the reputation to vote, but this answer is very confusing. (+1 for comments, if I could)

    avatar image mark.mckenna · Sep 17, 2012 at 01:02 AM 1
    Share

    @Bunny83, your complaint is a bit confusing because it addresses only syntactic problems in the answer and not the core of the answer. C# will toss a compiler error for both of these points, so I think they're pretty self explanatory. -- Also, as a potentially interesting aside, there is a conceptually valid interpretation for a virtual private method (which you correctly state that C# doesn't allow). Consider a situation where your parent class has a private abstract virtual method (still not legal in C#, but legal in C++). This specification dictates to an inheriting class that a function must be implemented for use by the parent; that the parent does not itself supply a default implementation for this function; and that this function need not be exposed for use by inheritors of the inheriting class. Forbidding the virtual private member requires the inheriting class to expose its implementation of the function to further inheritors, which isn't strictly necessary in this case, because the only reference to the v.p. member that should functionally exist already knows that a function of that name is accessible (via its own abstraction version of the member). -- Admittedly obscure, but I thought it interesting when I found out about it.

    Show more comments
    avatar image
    4

    Answer by Ricardo · Oct 24, 2009 at 04:10 AM

    Every script that you add to an object is itself a subclass of MonoBehavior, and from then on it's .Net all the way down (or up, actually). Here are two examples:

    1. As a really straightforward example, you could take a look at the code for Rock'n'Roll dice. As you can see, there's a DieController MonoBehavior, which contains most of the general code, and then there are some subclasses for the various types of die, which override a few relevant properties.

    2. You could also take a look at the behaviors included in UnitySteer, some of which inherit from a very basic VehicleBehavior and also implement particular interfaces.

    In short, any language feature that would apply to a regular .Net class applies to your Unity scripts as well, since they are simply .Net classes.

    Comment
    Add comment · Show 2 · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image Simon-O · Sep 03, 2015 at 08:48 PM 0
    Share

    Just to be a pedant, they're not simply .Net classes (I challenge you to get this == null to evaluate to True in stock .Net without some seriously sloppy coding)

    avatar image Dave-Carlile · Sep 03, 2015 at 09:19 PM 0
    Share

    I assume you're referring to Unity's custom == operator? Still .NET :)

    avatar image
    2

    Answer by Ralkarin · Jan 26, 2011 at 05:47 PM

    I just wanted to add that when using Inheritance for MonoBehaviour objects, the GetComponent() function on the GameObject class will return derived objects as well. This wasn't immediately obvious to me, but is very useful for deriving MonoBehaviours to share functionality and write abstract code in Unity.

    Using your scenario, here's a very simple example:

    public class CharacterBehaviour : MonoBehaviour { void Start() { CharacterBehaviour behaviour = gameObject.GetComponent<CharacterBehaviour>();

         print("Behaviour Type: " + behaviour.GetType().Name);
     }
    

    }

    public class PlayerCharacterBehaviour : CharacterBehaviour {

    }

    public class AICharacterBehaviour : CharacterBehaviour {

    }

    With a GameObject that has a PlayerCharacterBehaviour attached, you will see:

    Behaviour Type: PlayerCharacterBehaviour
    

    With a GameObject that has an AICharacterBehaviour attached, you will see:

    Behaviour Type: AICharacterBehaviour
    

    This lets you write generic code against all objects with a CharacterBehaviour attached.

    Comment
    Add comment · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image
    1

    Answer by bill · Feb 06, 2010 at 07:50 AM

    Inheriting from GameObject seems to crash unity. It would be nice to do to make it easier to interface with other code.

    Comment
    Add comment · Show 1 · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image yoyo · Dec 24, 2010 at 10:49 PM 3
    Share

    In Unity 3.0 GameObject is a sealed class, so you can no longer even attempt to derive from it. The same is true of other internal classes such as $$anonymous$$esh.

    • 1
    • 2
    • ›

    Your answer

    Hint: You can notify a user about this post by typing @username

    Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

    Follow this Question

    Answers Answers and Comments

    19 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    Does Unity support multiple Inheritance? 3 Answers

    The name 'Joystick' does not denote a valid type ('not found') 2 Answers

    Does Unity support the declaration of several classes in one script? 2 Answers

    Using GetComponent with a subclass 3 Answers

    Finding center of a cube 1 Answer


    Enterprise
    Social Q&A

    Social
    Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

    Footer

    • Purchase
      • Products
      • Subscription
      • Asset Store
      • Unity Gear
      • Resellers
    • Education
      • Students
      • Educators
      • Certification
      • Learn
      • Center of Excellence
    • Download
      • Unity
      • Beta Program
    • Unity Labs
      • Labs
      • Publications
    • Resources
      • Learn platform
      • Community
      • Documentation
      • Unity QA
      • FAQ
      • Services Status
      • Connect
    • About Unity
      • About Us
      • Blog
      • Events
      • Careers
      • Contact
      • Press
      • Partners
      • Affiliates
      • Security
    Copyright © 2020 Unity Technologies
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell My Personal Information
    • Cookies Settings
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Explore
    • Topics
    • Questions
    • Users
    • Badges