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
0
Question by Bamboy · Feb 22, 2013 at 03:29 AM · javascriptinheritanceclasses

Inheriting from classes from other files (Javascript)

I'd like to say before anything else that I'm still fairly new with programming in general. Especially when dealing with classes.

Basically in my setup that I have now, I have 'Entities.js' which contains all my functions and base classes that other scripts use. Other scripts use it by having 'import Entities;' at the top. This script is not meant to be put on a gameobject. In Entities.js I have a base class called 'CharacterBase'. It's the base class for the player, enemies, or anything that moves and can die.

When writing the code for the player in a separate .js file, I want the file itself to inherit from the CharacterBase class so I can then attach it to a gameobject. (Unity treats each script file its own unique class.)

I sort of tried to work around this issue when I first encountered it by just defining the player class within the file and then creating an instance of it. I ended up naming the file 'PlayerInit.js' instead of the intended 'Player.js'. As my project has been growing, I have quickly realized that it is going to cause a lot of issues. One of these issues includes not being able to get data from the attached gameobject or not being able to detect collisions. Another issue is other scripts having to go through multiple classes to call a single function.

I know that if I was doing this in C# none of this would be an issue because you must specify what the class the file inherits from. I rather dislike some of the syntax that C# uses, as well of how it instances objects, so I would really prefer to stick to Javascript if possible.


Below are the codes for my files.

If I have my class setups completely wrong, please tell me how I should be doing it. I'm using pastebin links because they are kind of lengthy.

**Entity.js**

**PlayerInit.js**

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

Answer by Eric5h5 · Feb 22, 2013 at 04:39 AM

The only thing different about Unityscript compared to C#, regarding classes, is that script files are automatically classes inheriting from MonoBehaviour. But you can ignore that if you want, and it doesn't prevent you from making other classes that don't inherit from MonoBehaviour. The only thing it does is make it easier to write scripts that include MonoBehaviour stuff like Update, but you can explicitly create a class that extends MonoBehaviour if you want.

However, judging from statements like "One of these issues includes not being able to get data from the attached gameobject or not being able to detect collisions", it sounds like maybe you shouldn't be using classes extending other classes in the first place, but should be using a component model. In Unity, you add Components to GameObjects, and can use GetComponent as needed. So instead of things like "blah = new Foo()", you Instantiate prefabs that have various scripts attached as desired.

As an aside, you don't really need those convert functions, since Unity already does that implicitly:

 var v3 = Vector3(1, 2, 3);
 var v2 : Vector2 = v3;

Or:

 var v2 = Vector2(1, 2);
 var v3 : Vector3 = v2;
Comment
Add comment · Show 3 · 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 Bamboy · Feb 22, 2013 at 05:03 AM 0
Share

Wasn't aware that you could do that with vectors.

$$anonymous$$aybe I wasn't quite clear on what I want. Basically I need the 'Player' class defined in 'PlayerInit.js' to become 'Player.js'. (PlayerInit is removed completely) This way I can attach the player class to a gameobject.

The issue I have is that I cannot tell unity that I want a class defined as a javascript file to inherit another class. It just defaults to inheriting $$anonymous$$onoBehaviour when it compiles. Where as in C# you $$anonymous$$UST say what the file inherits.

example.cs

 using UnityEngine;
 using System.Collections;
 
 public class example : $$anonymous$$onoBehaviour {
     void Example() {
         print( Application.loadedLevel );
     }
 }

example.js

 print ( Application.loadedLevel );

Both scripts cause unity to create a class called 'example'.

If I want 'example.js' to inherit from another class... Where do I specify that I want to do this?

avatar image Eric5h5 · Feb 22, 2013 at 06:55 AM 0
Share

The issue I have is that I cannot tell unity that I want a class defined as a javascript file to inherit another class.

Yes you can; as I said it works the same as C#, except that scripts are automatically classes that inherit from $$anonymous$$onoBehaviour (but you can ignore that if you want). These are the same:

 // Foo.js
 function Start () {}

 // Foo.js
 class Foo extends $$anonymous$$onoBehaviour {
     function Start () {}
 }

The second method is optional. If you don't want to inherit from $$anonymous$$onoBehaviour, you don't have to:

 // Foo.js
 class Foo {
     function Bar () {}
     // can't attach this script to a GameObject,
     // since Foo doesn't extend $$anonymous$$onoBehaviour
 }
avatar image Bamboy · Feb 22, 2013 at 10:53 AM 0
Share

Awesome! I fixed my scripts. Thanks for your replies.

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

10 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

Related Questions

Difficulty instantiating to a custom class 2 Answers

Defining and inheriting from a Javascript Class 0 Answers

How do I write this C# snipet in java script? 1 Answer

Using functions of Inherited classes 3 Answers

Function argument referencing variables? 1 Answer


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