how would i write this in unity script?

public CharacterController CharacterController { get { return m_Controller; } }

i’m quite good with unity script but i have no idea when it comes to C# could anybody translate this little tidbit for me?

thanks, Malarkey

The equivalent of this in C#:

CharacterController m_Controller;
public CharacterController controller { get { return m_Controller; } }

Is this in Unityscript:

private var m_Controller : CharacterController;
function get controller() : CharacterController {return m_Controller;}

In both cases, the property is used exactly the same way in code (even though you use the function keyword in Unityscript, it behaves like a variable). e.g.,

print (controller.name);

That script should give a compile error. I’m going to assume the code is this:

public CharacterController characterController { get { return m_Controller; } }
private CharacterController m_Controller;

Accessing the characterController of this script would return m_Controller, however that gets set. This allows the script to control how m_Controller gets set, but still allow other classes to access it.

EDIT: See Properties - C# Programming Guide | Microsoft Learn

What you are looking at is a C# property. They are a language construct built into C# for making getting and setting values easier. In that sense, they are halfway between being variables and functions.

See, in other C-style languages, you have the public, private, and protected keywords to describe what kind of access other classes or entities have to a class’ members.

For example:

class X
{
     public int a;

     public X()
     {
         a = 20;
     }
}

class Y
{
    void DoSomething(X x)
    {
         x.a = 3; // ALLOWED
         Debug.Log(x.a) // ALLOWED, prints out 3
    }
}

As you can see, class Y can access class X’s a member because it’s declared as public. If it was marked private, you’d get a compile-time error. The downside of marking it public is that class Y can modify the variable even if allowing that can break your code in class X.

Here’s an example of how one class can break another’s class’ functionality this way:

class X : MonoBehaviour
{
     public GameObject a;

     void Update()
     {
         Debug.Log(a.GetComponent<Transform>().ToString()); // will not crash if "a" is not null.
     }
}

class Y : MonoBehaviour
{
    void DoSomething(X x)
    {
         Debug.Log(a.GetComponent<Transform>().ToString()); // This is ok.

         x.a = null; //WHOOPS!! NULL REFERENCE EXCEPTION!
    }
}

As you can see, leaving it exposed can be dangerous. So sometimes, you don’t want a variable to be writable by other classes, but you still want to give other classes the ability to read the value.

This is where C# properties come in. They make it so that you can get a value or set a value, and both getting and setting can have their own access modifiers:

 class X
 {
      public int a
      {
           get;
           private set;
      }

      public X()
      {
          a = 20;
      }
 }

 class Y
 {
       void DoSomething(X x)
       {
           x.a = 3; // NOT ALLOWED
           Debug.Log(x.a) // ALLOWED, would print out 20.
       }
 }

Now that a is a property, it can be treated as if its a variable to other code, but its not really a variable. The private marker on set makes it so that only X can set a; this effectively makes it read-only to other code.

Properties are unique to C#, but the idea of making something read-only accessible to other classes is not. Many languages have ways for programmers to make their own “getters” and “setters”, even though the getters and setters themselves are not part of the language. For instance, languages like C++ work around not having built-in getters and setters by making a functions that only return values or constant references of the real value:

class X
{
public:
    int GetA() const
    {
         return mA;
    }

    X()
    {
         mA = 20;
    }

private:
    int mA;
};

class Y
{
     void DoSomething(const X& x)
     {
         x.mA = 3; // NOT ALLOWED
         std::cout << x.GetA(); //ALLOWED
     }
};

Likewise, you can take the same approach and just write a “get” function instead of a “get” property, which as you should see now, essentially equate to the same thing:

function GetCharacterController() : CharacterController
{
  return m_Controller;
}

private var m_Controller : CharacterController;