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…
var script; //Defaults to Object;
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
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.
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.