Static Function Aliases?

So I think you all know how “transform” is actually “UnityEngine.Transform”, and “renderer” was changed to “GetComponent.()” or “GetComponent()”.

How can I do this with my static functions? if I have a script named Func with a static function named “MyStaticFunc” then how could I make it where instead of doing “Func.MyStaticFunc(variables in here)”
I could just do “myFuncAlias(variables in here)” or something similar.

Thanks in advance! And no need to rush as this isn’t an important issue, but more of just a question. :slight_smile:

Apart from the misconception that other people explained, what you want to do can’t be done in Unity right now. There’s a new feature in C# 6 that’s exactly for that, you write "using static " and the class that has the static methods: Static Using Statement in C# 6.0 - IntelliTect

“UnityEngine.Transform” Refers to the namespace, dot the class. If you put using UnityEngine at the top of your file, you wont need to always specify the namespace and can call the class simply “Transform”.

The only place you will ever be able to call a static member of your class, WITHOUT specifying what class you are talking about, is INSIDE the class in question.

Class Func
   void Func1()
   {  StaticFunc();// since I'm "inside" the Func class, it assumes I'm talking about a member.
   static void StaticFunc()
   {... }

Class AnotherClass
   static void StaticFunc(){...}// since this class & function MIGHT exist, we NEED to specify the class when calling StaticFunc() from outside of these classes

If you want to extend MonoBehavior to include your own custom methods, such as an function that always attempts to fetch said Component, you can, either directly or through inheritance.

    class MyNewGameObject extends MonoBehaviour
        var myCustomClass : CustomClass;
        function Awake()
            myCustomClass = gameObject.GetComponent(CustomClass);
        function GetMyCustomClass()
            return myCustomClass;

You should be able to call this thru .myCustomClass, or .GetMyCustomClass();

*Note: the function is not static as you will be calling it on an instance of the MyNewGameObject.

You cannot alias functions in C#, the closest you can get is wrapping them in something else or maybe using delegates.

You can alias classes/namespaces/types with the “using” directive.

Also, if you do this kind of thing you end up with code only you will understand. And the you in 6 months time probably won’t understand it either :wink: