Dynamic Parameters/Arguments

Hi guys,

In my current project I’d like to pass on parameters in a dynamic fashion. I don’t want to use overloads since this makes the code extremely ugly and long for something that should really be very compact. I believe in C# 4.0 one can use the “dynamic” keyword to achieve this, but I am not sure how to make this happen in Unity’s C# 3.5.

In my specific case, I’d like to call a sync function which then executes a bunch of RPC calls depending on the dynamic parameter. This parameter could take a variety of types/values, e.g. string, float, Vector2, Vector3, etc.


private void Sync(string function, int id, int type, dynamic val) {
    if (val is float) ......

As mentioned, “dynamic” is unfortunately not possible in Unity at this point. What would be another, yet efficient way to achieve this?

I am assuming that the script calling the method will now the type. If so something like…

void Swap<T>(ref T lhs, ref T rhs)
    T temp;
    temp = lhs;
    lhs = rhs;
    rhs = temp;

Maybe that will work for your situation