Calling a function I don't know the name of?

Essentially, what I want to do is have an function in one script, have another script add a reference to it’s own function in the first script on Start(), so that whenever I call the function in the first script with a few arguments the function then sends those arguments to the function in the other script.

Here’s how I want it to work (in pseudo-javascript)

function DoSomething(){
    //stuff happens
    otherGameObject.GetComponent("Interface").Interact(player, 123.0f, "foo");
}

and then in the Interface script in otherGameObject

public ActualFunction : function;

function Interact(a:viewID, b:float, c:String){
    ActualFunction(a,b,c);
}

and last, the other script in otherGameObject

function Start(){
    Interface.ActualFunction = MyFunction;
}

function MyFunction(a:viewID, b:float, c:String){
    print(c+"bar");
}

I’m okay with using c# as well if that would be easier.

Well, what you want is a delegate. As far as i know it’s also possible in UnityScript now, but don’t ask me for the syntax :smiley:

In C# you would do something like that:

// define our delegate type
public delegate void MyDelegate(NetworkViewID a, float b, string c);

public class Interface : MonoBehaviour
{
    public MyDelegate ActualFunction;
    public void Interact(NetworkViewID a, float b, string c)
    {
        if (ActualFunction != null)
            ActualFunction(a,b,c);
    }
}

Never use the string version of GetComponent. In UnityScript, just write the component’s name without “”:

// UnityScript
otherGameObject.GetComponent(Interface).Interact(player, 123.0f, "foo");

Or use the generic version like you would in C#

// C#
otherGameObject.GetComponent<Interface>().Interact(player, 123.0f, "foo");

// In UnityScript
otherGameObject.GetComponent.<Interface>().Interact(player, 123.0f, "foo");

ps: I’ve found this old question on delegates in UntiyScript, but i’m not sure if it’s still up-to-date