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 /
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 which 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 this 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 which 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 which has no class body defined, you can create a class which inherits from it like this:

// --- 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 thing, It's important to note that when dealing with scripts which 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. (this 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 $$anonymous$$onobehaviour. 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

This code is going to be in C#, since I don't know JavaScript.

public class BaseThing : MonoBehaviour { // Class Code }

// Then, in another file

public class SomeThing : BaseThing { 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.

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

Display the speed of a Game Object in MPH 1 Answer

Using JSON to get tree data into JavaScript objects 3 Answers

Quit Menu errors 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