• 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
1
Question by Extrakun · Apr 01, 2010 at 05:42 AM · javascript-specificoop

Inherit from a behaviour (JS)

How do I create a base behaviour w$$anonymous$$ch other behaviour could inherit from?

Example:

// BasicBehaviour.js private var parentEntity_: Entity;

function Awake() { parentEntity_ = GetComponent(Entity);
}

I wish to have other MonoBehaviours inheriting from BasicBehaviour.js. How can t$$anonymous$$s be accomplished?

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
3
Best Answer

Answer by duck · Apr 01, 2010 at 08:56 AM

In Javascript, your base behaviour as you've written it should be fine. It can act as a base class as it is. Because you haven't explicitly declared a class body, Unity's Javascript compiler automatically adds it, and makes your script inherit from MonoBehaviour.

However to create a new class w$$anonymous$$ch extends your BasicBehaviour class, you need to explicitly declare the class body (otherwise it automatically inherits from MonoBehaviour), and specify that it should extend BasicBehaviour.

So, given that you have BasicBehaviour.js w$$anonymous$$ch has no class body defined, you can create a class w$$anonymous$$ch inherits from it like t$$anonymous$$s:

// --- ExtendedBehaviour.js --- class ExtendedBehaviour extends BasicBehaviour {

 // variable declarations inside the class body, here
 var myVar : int = 10;

 // and functions go inside the class body too
 function Start() 
 {
     Debug.Log("Extended Start");
 }

}

One last t$$anonymous$$ng, It's important to note that when dealing with scripts w$$anonymous$$ch inherit from MonoBehaviour (either directly, or indirectly), each class declaration needs to be in its own separate .js file, and the file must be named exactly the same as the class definition. (t$$anonymous$$s of course happens automatically when you don't define the class body yourself).

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 shohagmist · Jun 22, 2015 at 06:36 AM 0
Share

Unity 5.1 introduced Networkbehaviour which inherits Monobehaviour. Now I have to inherit Networkbehaviour for using the networking facility. What should I do for javaScript?

avatar image
0

Answer by qJake · Apr 01, 2010 at 07:45 AM

T$$anonymous$$s code is going to be in C#, since I don't know JavaScript.

public class BaseT$$anonymous$$ng : MonoBehaviour { // Class Code }

// Then, in another file

public class SomeT$$anonymous$$ng : BaseT$$anonymous$$ng { void Start() { print("I am a MonoBehaviour class."); } }

Derive your base class from MonoBehaviour, and derive other classes from your base class.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Extending from interface in JS? 3 Answers

Picking a language... 1 Answer

Resetting a combo attack chain if next attack did not occur in time 1 Answer

Field of view, using raycasting 5 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