- Home /
How does unity/javascript handle object references?
I just discovered that I could use this code to lock rotation to the initial rotation of a gameobject:
#pragma strict
var initialRotation : Quaternion;
function Start () {
initialRotation = transform.rotation;
}
function Update () {
transform.rotation = initialRotation;
}
All my previous experience is in pure Java, where this code wouldn't do anything. The object pointed to by transform.rotation would always be the same you point to in initialRotation.
How are object references handles in Unity? Do they exist at all, or are all values simply copied?
Transform is an object, your object has one. (every "GameObject" in Unity has a Transform, it's the one thing you can't not have. an "empty" GameObject is just a Transform.) rotation is a property of "Transform".
By the time Awake() runs, not to mention Start(). the Transform (and indeed it's rotation, and other properties) have already been set to something. Start() happens relatively late.
note that experts on here are always pointing out "Unityscript is not Javascript".
this may be relevant .. http://answers.unity3d.com/questions/261419/you-own-structs-passed-by-reference.html
Answer by Eric5h5 · Sep 28, 2012 at 04:19 PM
Classes are by reference, structs (such as Quaternion) and primitive types are by value.