Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

c# Extension class help understang (very simple)

Discussion in 'Scripting' started by ApteryxK, May 30, 2015.

  1. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    Hi, I am currently learning Extension Classes and from what I could undertand :
    You have to create a name space, then a static class and a static function, the keyword this is used in the parameter of the extension function, but I don't understand where we say of which class it is an extension.
    Namespace A{
    public static class GameObjectExtensions
    {
    public static void NewTrick (this GameObject go, Vector3 pos)
    {
    go.transform.position = pos;
    Debug.Log(go.name);
    }
    }
    this GameObject go
    "this" is telling unity that it is an extension, (here we want it to be an extension of Gameobject class) but just after that we say GameObject go, here we can see that GameObject belongs to go, it is its type.
    So where do we say what it is the extension of ? I don't understand, or I am missing something...
    Thank you very much :)
     
  2. Deleted User

    Deleted User

    Guest

    You don't have to create a namespace to use extension classes.
    It is an extension of the class which you use the 'this' keyword on. So in your example, it is an extension of GameObject (although it seems kinda pointless tbh).

    GameObject doesn't belong to go. go is just a reference to an actual GameObject. go is not a type. The type of GameObject is GameObject.
     
    ApteryxK likes this.
  3. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    I was trying to do this in another class.
    I still don't understand, if you have another class and do all the stuff, how does it know of what class it is an extension of ?
     
  4. Deleted User

    Deleted User

    Guest

    Well really, extension methods are just syntactic sugar. So when you do this:

    Code (CSharp):
    1. public static void NewTrick(this GameObject go, Vector3 pos){
    2. go.transform.position = pos;
    3. }
    It is the exact same as this:

    Code (CSharp):
    1. public static void NewTrick(GameObject go, Vector3 pos){
    2. go.transform.position = pos;
    3. }
    The end result is the same but the syntax is a bit different. To use the former, you would do this:

    gameObject.NewTrick(Vector3.zero);

    The latter:

    NewTrick(gameObject, Vector3.zero);
     
    ApteryxK likes this.
  5. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    Thank you, if I understood right :
    "(this GameObject go)"
    Ok, so here the word (gameObject) after "this" keyword tells of which class it is an extension.
    But GameObject also defines the type of "go".
    So GameObject is used twice ?
    But this is causing a flexibility problem...
     
  6. Deleted User

    Deleted User

    Guest

    go is not a type. It is a variable.

    You could do this:

    Code (CSharp):
    1. public static void Attack(this GameObject attacker, GameObject defender){
    2. // Do Stuff
    3. }
    then you would just do
    gameObject.Attack(otherGameObject);
     
    ApteryxK likes this.
  7. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    So what type is attacker here ?
     
  8. Deleted User

    Deleted User

    Guest

    attacker is a GameObject.
     
    ApteryxK likes this.
  9. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    So GameObject also defines the type of attacker.
    But it is still flexible as you can still put another argument and not use the first.