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
2
Question by gRntaus · Apr 07, 2013 at 06:59 AM · classmonobehaviour

Can a Monobehaviour extend a class?

As the title states I am looking to rewrite code here as much as possible and in some cases I want a few different methods and a change in a few variables but I want the bulk of the work to be the same. It took me a while to work out what I wanted to do / how to ask the question but I think the above is the most simple version.

In c# can I create a normal class structure and then get one of my Monobehaviour scripts attached to an object to extend that class and thus get all it's functionailty? Thanks!

Comment
Add comment · Show 1
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 Fattie · Apr 07, 2013 at 07:15 AM 0
Share

does this sort of answer help ?

http://answers.unity3d.com/questions/430582/extending-a-singleton-base-class-ujs-.html

suggest ou search for 100s of questions on the topic you ask ...

1 Reply

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

Answer by EliteMossy · Apr 07, 2013 at 07:23 AM

Say you have a class called Agent you can derive from this class and create 2 classes called AgentFast and AgentSlow.

Obviously you want to attach AgentFast and AgentSlow to a GameObject. Well to do that you can do:

 public class Agent : MonoBehaviour{
 }

then to derive from it you do

 public class AgentFast : Agent {
 }
 
 public class AgentSlow : Agent {
 }

And you get full access to the MonoBehaviour in the base class.

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 gRntaus · Apr 07, 2013 at 07:31 AM 0
Share

Ahh, O$$anonymous$$ thanks that makes more sense. I'll muck around with it tonight and see if I can't get some basic functionality working.

avatar image EliteMossy · Apr 07, 2013 at 07:35 AM 0
Share

Just a note, if you are using Awake, Start, Update etc in the base class, then you decide to use the override keyword ins$$anonymous$$d of new, you will need to do base.Awake or base.Start etc to process the base

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

12 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

Related Questions

Is it possible for a non-MonoBehaviour class to use built-in functions? 1 Answer

new Monobehaviour as variable returns warning... 1 Answer

How do I use "helper classes" in C# and have them show up in the Inspector? 1 Answer

Monobehavior class variable become null after assigning value when instantiated 1 Answer

Can I call a class's method which inherit monobehaviour by a normal class? 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