how can I create a variable to refer to any gameObject script with pragma strict

I want to create a global variable in pragma strict that refers to the script attached to any game object. Is this possible?
scriptTypes are script1,script2,script3 etc…

 #pragma strict;
    var script; //Defaults to Object;
    

function GetProperty(go:GameObject,scriptType:?){
    script=go.GetComponent(scriptType);
    Debug.Log(script.property1);
    }

That'd be class script1 extends myClass{} in JS.

Are you saying in javascript I would write class scriptType{ var script1:script1; var script2:script2; } var script:scriptType; I'm confused about how this would help.

syclamoth: can scriptType be a variable that I can pass into a function? So I could do. function getProperty(go:GameObject,scriptType:Monobehavior){ script=go.GetComponent(scriptType); script(as scriptType).property=10; } or something like that...?

1 Answer

1

There are two things that you’re trying to do here, and they kind of conflict.

If you have a variable that you want to be able to refer to any script, it’s simple enough to just go-

var script : MonoBehaviour;

But, this won’t allow you to access

script.property1

because that is specific to a certain subclass. Because at that point in the program, that object could be anything, there’s no way that the compiler can know exactly what type you expect there, so it won’t allow you to use it that way.

There are a few ways around this. The simplest is, if you (the programmer) know what the type is expected to be, to just manually cast the object into the correct type.

script(as scriptType).property;

Keep in mind that this won’t work if you get it wrong.

The other way you could go would be with @NOAA_Julien’s solution, but this puts a hard limit on the types that you can use with the system- so you have to be aware of the limitation. Also keep in mind that using this architecture can screw around with the serialization- which isn’t a problem if it only needs to work at runtime, but make sure it doesn’t catch you unaware.

var script:MonoBehaviour; Is there a javascript equivalent of this line: script(as scriptType).property; or for typecasting script below as a cubescript? script= GameObject.Find("Cube").GetComponent(cubescript);

I think the generic version should work properly- var script : cubescript = GameObject.Find("Cube").GetComponent.<cubescript>(); that or var script : cubescript = GameObject.Find("Cube").GetComponent(cubescript) as cubescript;

I'm running into trouble. I'm trying to set the script once globally depending on the object -- so that all other functions can use the script. var script:MonoBehaviour; function getScript(name,type){ if ( name=="cube"); var script:cubescript= GameObject.Find("cube").GetComponent(cubescript) as cubescript; } function setProperty(){ getScript("cube","cubescript"); script.property1=20; Debug.Log(script.property1); } setProperty();

also how are you highlighting the code in a comment. I couldn't find any info about how to do that...

Thanks for the comment trick. Is it possible to add variables to a script after it is running. Like set a property or method afterwards?