so i have been building a project in Unity for a while now and have run into a problem:
when i am writing generic scripts (such as proximity triggers) i want the code to call different outside scripts - as such i want to declare a variable which will act as the script so i can call functions / change variables
var myScript: Script;
myScript.doStuff = true;
the way i have gotten around this problem recently is to do the following:
var myScript : MonoBehaviour;
myscript.Invoke("myFunction", 0);
this works just fine, but is a little unclear in my opinion, and as i want other programmers to understand this, i was wondering if there was a datatype for script
much thanks
You would need to add an extra layer of inheritance between monobehaviour and your scripts.
Then you can add a public bool doStuff and use that to trigger specific routines in their updates. Or provide overrideable functions that will perform different operations dependent on the class.
Although I would suggest using Invoke as it is significantly clearer and easier to debug. With Invoke you know to question the class type, with generic overrideable parameters it will be more obscure that each call to that function will perform different actions.
Edit:
As far as creating generic callback triggers, what I did was use SendMessage without requiring a receiver.
Then I can have multiple callback scripts within that gameobject respond to an event. The Trigger itself remains generic, but the secondary scripts attach to the trigger are what perform the unique actions.
you can just use the name of the script as the type. for example if you had a game object with a script named “ProximityTrigger” with the function doStuff() and the var stuff which you wanted to access from another script, you could do:
var pt : ProximityTrigger;
//...
pt.stuff = true;
pt.doStuff();
then you can drag any object with the ProximityTrigger script attached to the pt var in the inspector, or assign it in some other way if you prefer.
if you want to use more than one kind of script, you can use SendMessage as described above.