Script access (without script name)

Hello, I need to get all scripts on a gameObject, and then to get a MyVariable in each of them. Something like this:

    function Start(){
var components = GetComponents(MonoBehaviour);
for (var script: MonoBehaviour in components){
 print(script.MyVariable);
}
}

(this gives me errors, of course). I can't use Unity right now, so there can be stupid mistakes in the code, but i hope it helps to understand what i'm trying to do. Thanks in advance!

your best bet would be to make a parent class with your variable on it and extend that from the other scripts - that way you can use GetComponents(BaseClass) instead, and you have access to the scripts which matter

something along the lines of this , though best to use a seperate js file for each


class BaseClass
{
    var MyVariable : String;
}

class MyExtendingClass extends BaseClass
{
    //rest of your normal code here, you can use MyVariable from inside here just fine
}