Unity getter properties

Hello, Community!
I’m reading this article:
There is such phrase: "FindObjectsOfType (and Unity getter properties in general)" in this manual.

I can’t understand what is Unity getter properties?
May be anybody exactly knows?
Thanks in advance.

Getters and setters are a general name for methods which handle getting and setting (bet you didn’t expect that!) values from and to fields.

In C#, getters and setters are implemented as a syntactic feature, which allows you to abstract the field.

i.e. all of the following are functionally identical.

// A
public string name { get; set; }

// B
public string name { get { return _name; } set { _name = value }
private string _name;

// C
private string _name
public string GetName(){ return _name; }
public void SetName( string name ){ _name = name; }

Also, if you’re using Visual Studio or MonoDevelop, properties (fields accessed using getters and setters) are usually denoted with a special icon which is neither a variable nor a method.

What this means basically is that it’s easy to mistake a function call for an assignment - consider this:

myobject.age = 15

Is this an assignment operation? a method call? it can be either, depending on wether age is a variable or a property.

Going back to the context of Unity, what this means is that some of the getters defined in the Unity API are basically wrappers for more expensive lookup methods. MonoBehavior.rigidbody, .transform, .camera, etc all basically wrap a GetComponent() call and are not cached values.

So basically, what this means is that if you have this piece of code:

void Update()
    rigidbody.AddForce( force );

You’re basically calling GetComponent< Rigidbody >() on every update, since rigidbody is a getter which returns the result of GetComponent. Those lookups are more expensive than simply storing a reference, hence the manual suggests you should be aware of such scenarios.

The best thing to do is to cache a reference to components you need to look up often - however, it’s important to keep in mind that if you do store your own references you need to be aware of their lifecycle and validity.

an example for such pitfall:

private Rigidbody m_cachedRigidbody

void Awake()
   // Let's assume there's no rigidbody attached on awake
   m_cachedRigidbody = GetComponent< Rigidbody >()

void Start()
    AddComponent< Rigidbody >();

void Update()
    // This is valid since at the time the lookup is executed a Rigidbody component exists
    rigidbody.AddForce( force );

    // This will throw a null reference exception, since there was no rigidbody attached when we stored the result of GetComponent< Rigidbody >() so m_cachedRigidbody is actually null
    m_cachedRigidbody.AddForce( force );

I believe it basically means all methods in Unity that get a list of objects or an object from the scene by name/type/tag/component etc. where the only way to find those objects is to (internally, hidden from the user) loop through a set of objects/components and check if they meet the given criteria.

This would include especially such as GameObject.FindGameObjectsWithTag, Object.FindObjectsOfType and GameObject.Find and to certain extent GameObject.GetComponent and Transform.Find.

Of course the structure of you objects/scne also affect this