Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Vekosava · Jul 03, 2016 at 11:48 AM · gameobjectinheritancemonobehaviourattach

Does my base class that inherits from MonoBehaviour needs to be attached to an gameobject within a scene?

Hey guys! I've worked in C# in Unity for few months now, but without using inheritance at all. :D Today I decided to start learning it, I understand a lot now, but one simple (?) thing I don't (before I start implementing and refactoring my code), it's "Does my base class that inherits from MonoBehaviour needs to be attached to an gameobject within a scene?". I know that base class contains all required variables,methods for other scripts that inherits from it, but, if answer to my question is Yes, then where I should attach it?

I hope someone can answer this! BTW Thanks to Unity Community for helping me a lot! :D

Comment
Add comment
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

1 Reply

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

Answer by NoseKills · Jul 03, 2016 at 04:33 PM

It's sometimes pretty hard to understand what people learning inheritance are actually asking because it's so easy to misunderstand inheritance in such complicated ways...

So if you make classes

 class Base : MonoBehaviour {
    public int baseInt;
 }

 class Extend : Base {
    public int extendInt;
 }

what i think you are asking is, do you need to attach both of these scripts to gameObjects to take advantage of inheritance.

The answer is no.

That's because in a sense there are no 'two scripts' here. When you extend a class, it basically means everything declared in the base class gets merged into the extending class when you create an instance of the extending class.

You can almost think of inheritance as a text editing feature instead of anything more complicated.

If you now drag the Extend script into a gameObject you effectively have a script like this on the object

 class Extend : MonoBehaviour {
        public int extendInt;
        public int baseInt;
 }

Related to this, another thing that confuses peolple is that the instances of base script and instances of extending scripts are not magically connected in any way.

If you drag the Base class onto one object and Extend class to another object, then run the game, the scripts are separate instances despite of inheritance. If you change baseInt on one object, that variable doesn't change in the othe objects because they are separate instances. Each of them has their own baseInt despite inheritance.

If you make a base class 'Mammal' and put all the 100 methods and variables required to describe a mammal there and make an extending class 'Horse', that's just a way to avoid having to write those 100 methods and variables again when you make the 'Cow' class. When you make a game with those classes, you probably only attach 10 Cow and 10 Horse scripts to different objects and they will each have their own 100 mammal variables. You don't need to have separate Mammal scripts on objects in the scene to make things work. The mammal stuff is already in each of the cow and horse objects because of inheritance.

Comment
Add comment · Show 4 · 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 Vekosava · Jul 03, 2016 at 04:49 PM 0
Share

So I can leave my base class in my assets folder and just use it to inherit data?

avatar image NoseKills Vekosava · Jul 03, 2016 at 06:05 PM 1
Share

Yes. All of the C# classes you make are a part of the C# solution of the Unity project and get included in the builds you make. What you drag'n'drop and where in Unity editor is a separate thing and doesn't really concern the C# project

avatar image Vekosava NoseKills · Jul 05, 2016 at 09:28 AM 0
Share

Thanks a lot!

avatar image pslattery · Jul 30, 2017 at 06:20 PM 0
Share

This is actually a great initial explanation of inheritance! Will be using this when I $$anonymous$$ch from now on!

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

69 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 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 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 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

Unity Inheritance and Monobehaviour 1 Answer

Attach a shield to player (C#) 2 Answers

How to inherit from a class that inherits from MonoBehaviour 1 Answer

How to make RequireComponent list possible options. 1 Answer

Child of child of MonoBehaviour isn't a MonoBehaviour? 0 Answers


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