Transform Info and Classes

Hi i was wondering if anyone could help me here .
I often see in tutorials and scripts that transform is sometimes spelled with an uppercase letter and some times spelled in lowercase can some one please tell me why.??

And also what is the difference between a class function and a class variable??

Thanks for ur help in advance

transform is a property of MonoBehaviour which references to the Transform attached to the same GameObject as the script itself.

While “Transform” is a type (class). You use Transform to store a reference to a certain transform (child, transform assigned via inspector, or obtained via transform.Find method, etc.)

In other words, when you say just ‘transform’, you are making a shortcut to the Transform component of the GameObject that this particular script is attached to. It’s a class variable on the script you’re writing (holding an instance of the Transform class). When you can’t actually see the variable being declared at the top of the script, it’s because it’s declared in MonoBehaviour (the parent class).

When you say ‘Transform trans = someGameObject.transform’, you are creating a reference to the transform component of another GameObject. Now ‘trans’ is an instance of the Transform class.

Good luck!

There are two small errors :smile:

  1. transfrom is no part of MonoBehaviour, but of Component, which is indirectly part of MonoBehaviour too.
    The inheritance tree is something like
    object
    |-UnityEngine.Object
    | |-Component
    | | |-Renderer
    | | | |-MeshRenderer
    | | |-Behaviour
    | | | |-MonoBehaviour

As a proof, when you have something like

Rigidbody rigidbody = GetComponent<Rigidbody>();
rigidbody.transfrom = new Vector3(0,0,0);

Rigidbody is not derived from MonoBehaviour, as you can see in the docs, but from “Component”.

  1. transfrom is a property
public class ComponentDemo : Object{
    // Private variable
    private Transfrom _transform;

    // A property which exposes the transform to the public. It checks if its saved and use the gets it if it's obtained yet
    public Transform transform {
        get {
            if(_transform!=null)
                _transform = GetComponent<Transform>();

                return _transform;
            }
        }
        /*
        // Example of a setter, which doesn't exist in the real Component class
        set {
            _transform = value;
        }
        */
    }
}
// disclaimer
// Of course this is not how the real Component class works
// in reality, the component class's getter calls an internal method of the Unity3d Engine, 
// written in C++ and not in Mono, an InterOP call which is somewhat more expensive 
// than the code above, but is more reliable than that
// check http://forum.unity3d.com/threads/130365-CachedMB for more indepth info 
// about caching, the "shortcut"-property and the performance

As you see, the variable itself is private and can’t be directly accessed. The property (which is actually kind of a getter/setter wrapper) has a getter, but not setter defined. This means, you can read the transform, but you can not set it

// doesn't work
transform  = someOtherTransform;

In reality, the Property compiles into two function calls

public class ComponentDemo : Object {
    // Private variable
    private Transfrom _transform;

    // A property which exposes the transform to the public. It checks if its saved and use the gets it if it's obtained yet
    public Transform get_transform() {
        if(_transform!=null)
            _transform = GetComponent<Transform>();

            return _transform;
        }
    }

/*
    // a hypothetical setter, see above
    public void set_transform(Transform newTransform) {
        _transform = newTransform;
    }
*/
}

As you see, properties just makes your C# code pretty (same for unityscript, just that Unityscript doesn’t have properties, it can only use them but not create own ones).

Without a property a code with getter/setter would look like this (this is what Java does… Not JavaScript or UnityScript)

Transfrom t = get_transform();
set_transform(newTransform);

// or if you have a number property
set_health(get_health() + 10);

// with properties the same code looks like

Transfomr t = transform;
transform = newTransform;

health += 10;

It’s important to remember that all the “shortcuts” as you call them in the Component class are no variables, but “getters” (a getter/setter on the other side are methods). A getter/setter can execute more or less code, or even add validation

private int health;
private int maxHealth;

public int Health {
    get {
        return health;
    }
    set {
        if(health < 0) {
            health = 0;
        } else if(health > maxHealth) {
            health = maxHealth
        } else {
            // set health only if its valid. We can't have negative health and we can't more than max health
            health = value;
        }
    }
}

This way you can’t “accidentally” set health to a invalid value. It’s “basic knowledge”, at least when you are C# programmer