# Problem with Scripting

I’m trying to write a script that sets the rotation of an object to 0,0,0 if the rotation of any axis is more than 20.
I keep getting an error. Can someone help me? Here is my script:

var xRotation = transform.rotation.x;
var yRotation = transform.rotation.y;
var zRotation = transform.rotation.z;

function Update () {

if(20 < xRotation){
transform.rotation(0,0,0);
}

if(20 < yRotation){
transform.rotation(0,0,0);
}

if(20 < zRotation){
transform.rotation(0,0,0);
}
}

The transform component of a object is stored as a quaternion, not a simple Vector3. (It is also a property, not a function.) Unless you are skilled at using non-commutative imaginary numbers, I would suggest using Unity’s built in Quaternion functions.

Quaternion.eulerAngles returns the quaternion as x, y, and z rotations.
Quaternion.identity is a static property representing no rotation. Assigning it to the rotation of a transform is like setting it to 0;

``````var xRotation = transform.rotation.eulerAngles.x;
var yRotation = transform.rotation.eulerAngles.y;
var zRotation = transform.rotation.eulerAngles.z;

function Update () {

if(20 < xRotation){
transform.rotation = Quaternion.identity;
}

if(20 < yRotation){
transform.rotation = Quaternion.identity;
}

if(20 < zRotation){
transform.rotation = Quaternion.identity;
}
}
``````

This should work:

``````var xRotation = transform.eulerAngles.x;
var yRotation = transform.eulerAngles.y;
var zRotation = transform.eulerAngles.z;

function Update () {
if(20 < xRotation){
transform.eulerAngles = Vector3(0,0,0);
}

if(20 < yRotation){
transform.eulerAngles = Vector3(0,0,0);
}

if(20 < zRotation){
transform.eulerAngles = Vector3(0,0,0);
}
}
``````

Though you can simplify it by writing:

``````function Update() {
if(20 < xRotation || 20 < yRotation || 20 < zRotation) {
transform.eulerAngles = Vector3(0, 0, 0);
}
}
``````

Also, you are never updating xRotation, yRotation, or zRotation. You may have left it out on purpose, but if this is unintended, move them into the update method as well.

Thanks a lot guys. This was extremely helpful. You have both helped in the production of what will hopefully become a published game.

Doh! Sorry, eulerAngles is a Transform property, not a Quaternion property. My bad =]. Thanks for the save Dman lol

It works perfectly. Thanks a lot.