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

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;