Okay, I’m hopelessly lost with figuring out how inheritance works in Unity JS.
Here’s the setup: I’m making an RTS engine where the player can control different units. I have an abstract class called “Unit” and two inheriting classes called “Tank” and “Ship”. In Unit.js I define a function like so:
virtual function moveTo() {
...
}
Now, when I’m Raycasting to see which unit the player clicked on, “Tank” or “Ship”, I get a reference to an instance of the Tank object or Ship object. Once I get that reference, I want to be able to call the moveTo() function on it, but how? I know it’s supposed be in this form:
reference.scriptName.moveTo()
But if I say
reference.Unit.moveTo()
I get an error. And I can’t say
reference.TankScript.moveTo()
because the reference might be a ship, and that would throw an error.
I’m an ActionScript 3.0 guy and the fact that Unity objects can have multiple scripts is totally throwing me off. No matter how I try, I can’t seem to access functions dynamically across various objects. It’s like a catch-22: to access moveTo() I need to know the object’s script name. But I can’t know the script name because depending on the object selected, it changes.
How do you guys deal with this problem? It’s impossible to code anything without resolving it.
Also, when you guys speak of “classes”, are you talking about the script or the object? And for the above example, is it necessary to have the abstract Unit object actually in the Hierarchy (in the game world), or is it simply enough to have a script called Unit?