Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by RoR · Oct 30, 2010 at 08:09 AM · classinheritancemonobehaviour

C# Question - Do I Have to inherit from MonoBehaviour? What happens if I don't?

If I make a class Inventory that "does" not inherit from monobehavior.

and this class is where i keep track of my players items.

Can I instantiate this class In my player class like so?

Inventory bag = new Inventory(); // ?

Also every script in unity requires to be on a gameobject ? so I can't just have classes built and use them within the code?

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

2 Replies

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

Answer by duck · Oct 30, 2010 at 10:47 AM

Yes, your classes don't have to inherit from MonoBehaviour, and you can instantiate them in the normal way (using 'new').

Inheriting from MonoBehaviour simply gives your scripts extra abilities, such as being able to:

  • be placed on gameobjects
  • have public vars which can be adjusted in the inspector
  • receive Unity events such as Start() Update() FixedUpdate() etc.

It's worth noting that while you can instantiate other classes using 'new', you shouldn't use 'new' to create new instances of your MonoBehaviour scripts. This is because there are many areas where the functionality of the Monobehaviour relies on assuming that it's attached to a GameObject, and if you create a new one without it being attached to a GO, it can cause it to malfunction and give errors.

For this reason, to create a new instance of a MonoBehaviour at runtime, you should always use AddComponent to create and attach it to an existing gameobject at runtime.

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 DDeathlonger · Dec 19, 2017 at 12:24 PM 0
Share

It also removes abilities which is getting REALLY FRUSTRATING. I.$$anonymous$$ the ability to have a base class inherit from $$anonymous$$onobehaviour, then a subclass that inherits from that, is stuck with monobehaviour and can no longer add new instances of classes to a list in the parent (management class) via the "new" keyword..

avatar image
1

Answer by fherbst · Oct 30, 2010 at 09:16 AM

Yes, you can do that if you want - but in many cases, it's easier and better to use MonoBehaviours.

Scripts aren't gameObjects - they are Components - and this is only required for scripts which can be added to gameObjects as Components (that is, in the Editor or via AddComponent) and which have Update and Start and so on called automatically.

But yes, you can use classes and stuff the same way you would in "non-unity3d" programming.

What you can do, too, is to inherit from a class which is a MonoBehaviour (you have class A : MonoBehaviour -> you can create class B : A and it can be used in the editor).

Oh, and maybe you have to put your custom classes in a folder called "Plugins" to be accessable from other scripts ('cause of compilation order).

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 duck ♦♦ · Oct 30, 2010 at 10:41 AM 1
Share

No, you don't have to put custom classes in a separate folder - that's only for if you're mixing languages (eg, trying to use Javascript and C# classes together). If all your code is in the same language, they can all access each other just fine.

avatar image fherbst · Oct 30, 2010 at 12:07 PM 0
Share

Okay, than I mixed that things up a bit :). Thanks for clarifying.

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

2 People are following this question.

avatar image avatar image

Related Questions

C# Conception - Hide inherited members and functions 1 Answer

C# Inheritance, base class attributes, override and null object 1 Answer

Inherited class, trying to overwrite variable. 0 Answers

Can I call a class's method which inherit monobehaviour by a normal class? 0 Answers

Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 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