- Home /
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!
I have the same issue, the answer is for C# so I can't use it for UnityScript. The above script should work, why doesn't it?
Answer by Mike 3 · May 07, 2010 at 11:29 AM
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 }
Thank you! I'll try to do it the way you're suggesting, though I'm still learning, so it's still hard to grasp how to use classes in unity. (I'm trying to go through all the scripts on a gameObject, and check $$anonymous$$yVariable, just so i could add any scripts to that object, and as long as they have that variable, they're functions will be called in order i like. Something like behaviors with priority variables.)
that's difficult! it seems to run okay, but if I try to do something in the class like print something it doesn't seem to happen.
Do you mean that i have to make some kind of Class thingy (sorry) within each of the scripts that is to be switched on or off from the scriptcontrol script? and then it will make a variable that can be accessed in any script? sorry i just put it all in the same script at the moment i didnt understand:
class BaseClass
{
var $$anonymous$$yVariable : String;
}
class $$anonymous$$yExtendingClass extends BaseClass
{
function Start(){
var schemeobject = GameObject.Find("schemeobject");
var components = schemeobject.GetComponents($$anonymous$$onoBehaviour);
for (var script: $$anonymous$$onoBehaviour in components){
print(script.name);
}
}
}
I think at the end of the day this is an error in unity, if you can't make an array of scripts like any other array, because it throws up errors, it makes things difficult for programmers. i ve read all the forum posts on it, and worked for 2 3 hours... $$anonymous$$onoBehaviour[] is not usable.
ZoomDomain, you are using print in a class not derived from $$anonymous$$onoBehaviour. This will not work as $$anonymous$$onoBehaviour implements print; your class does not have the function available as it doesn't inherit from $$anonymous$$onoBehaviour.
Use Debug.Log() ins$$anonymous$$d.
Note that the important thing is the type of class you are calling print in. It does not matter that your script variable is cast as type $$anonymous$$onoBehaviour, $$anonymous$$yExtendingClass must be (derived from ) $$anonymous$$onoBehaviour when using print.