There are two small errors
- 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”.
- 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