Inheritance js/c#

hi guys, i’ve got the following issue

Character.js

var move:boolean = false;

function FixedUpdate
{
  if(move == true)
  {
    transform.position.z += 0.1;
  {
}

now i made 2 classes that inherit from Character, lets say fighter and archer.

so naturally when i set the the move variable on one of those 2 classes to true, i want the corresponding object to move. however, when i do that, ALL the objects inheriting from Character move.

first i thought that the fighter and archer class have a problem with inheriting the FixedUpdate, but changing it to a Move() class and calling it from the newly created FixedUpdates from the fighter/archer class did exactly the same.

now i’m sure there’s a simple solution to this and i’ve got something very basic really wrong. is this a js-only issue and can it be solved using C#? or did i just make some mistake?

and btw., what are the big advantages of using C#? i just can’t find any usefull document explaining that…

in C# its pretty simple

Base class declares the function as Virtual blabla

extended class then does Override blabla and call the original function as first within the overriden function

No idea how that stuff works in JS thought

There’s no support for virtual/override fields and properties in JavaScript. Boo behaves a bit better but it still doesn’t work as you would expect.

The only language with working virtual/override/new support is C# and even then, overriding public variables will confuse the Unity Editor.

http://forum.unity3d.com/viewtopic.php?t=10620

now ive rewritten my code to c-sharp, but im still new to this stuff… could you be a little more specific with “call the original function as first within the overriden function”. isn’t it already overwritten then? how can i call it from the inheriting class? what is “as first”?

thx

This works perfectly:

have you tried make multiple instances of fighter? they will all move at the same time if you set the move variable to true on one fighter. what i want is to control the fighters individually (by setting each owns move variable to true, and only have that one move).

You need to change the value for one spcific instance either in the Hierarcy (checking move checkbox) or by code

function OnGUI()
{
  if (GUI.Button(new Rect(200, 10, 100, 20) , "Move Fighter"))
  {
    GameObject.Find("Fighter").GetComponent("Fighter").move = true;
  }
}

I am guessing you change it in the prefab or directly in the code?

i just found the problem, i declared the move variable as static, and somehow he didn’t like that. now it works like a charm.

why is that a problem with making it static?

Because static variables are not per instance (object) but per class. That’s what static means.

yeah right i thought there was something :slight_smile: i was so used to make variables static so that i dont have to click reset in the inspector each time

thx a lot