How come I don't have to type "GetComponent(transform)()" but instead I can just type "gameObject.transform"?

using UnityEngine;
using System.Collections;

public class testTransform : MonoBehaviour {
    public Transform tran;
    void Start () {
        tran = GetComponent<Transform>();

How come I don’t have to type “GetComponent(transform)()” but instead I can just type “gameObject.transform”?

Is it because of MonoBehaviour or UnityEngine that’s allowing me to take this “short cut”? Thanks!

Unity’s MonoBehaviour class includes a few properties that pretty much syntactic sugar for quick GetComponent calls. Something roughly like this:

public Transform transform {
	get {
		return GetComponent<Transform>();

Several similar properties exist to look up an attached AudioSource, Camera, Renderer, Rigidbody, or so on. See the scripting reference for more details.

Any behavior script you write must inherit from MonoBehaviour, so most of your scripts will also have access to these shortcuts.

If you do decide to write your own helpers, I recommend adding a cache step to avoid unnecessary extra calls to GetComponent.

The Component class has getters for most of the built-in components. Quite literally:

public class Component : Object {
    public Transform transform {
        get { return GetComponent<Transform>(); }
    // etc

Note, that MonoBehaviour is a class inside of the UnityEngine. MonoBehaviour inherits from Components. So to answer your question, it’s both because of MonoBehaviour, UnityEngine as well as Component.

Also note, that GetComponent is actually an alias for gameObject.GetComponent. A bunch of functions are like this and exist purely to make things a little bit easier.