- Home /
The question is answered, right answer was accepted
Set eulerAngles to c#?
I try to write my scripts from now on in c# instead of javascript and have some little problems converting a script.
transform.eulerAngles.x = Camera1.eulerAngles.x - 90;
I always get the error that I can't set eulerAngles.x because it isn't a variable. How can I set a single Angle in c#?
Answer by Mortennobel · Apr 28, 2011 at 08:12 PM
Vector3 tmp = transform.eulerAngles;
tmp.x = Camera1.eulerAngles - 90;
transform.eulerAngles = tmp;
Reason: The eulerAngles is a property, and you cannot set only part of a property (transform.eulerAngles.x would invoke the getter method - which cannot be used as a setter). In other words: You need to use a temporary variable to do what you want to do :-)
Is it the same for the y angle? I just need to change the .x to .y?
Answer by Antony-Blackett · Apr 28, 2011 at 08:15 PM
I think c# is a little strange in that you can't set a property on a property of an object if the first property is a value type. Do this instead.
Vector3 eulerAngles = transform.eulerAngles;
eulerAngles.x = Camera1.eulerAngles.x - 90;
transform.eulerAngles = eulerAngles;
haha thanks, but not deserved. $$anonymous$$y code was wrong (fixed now) and the $$anonymous$$ortennobel beat me while typing. :P
The reason is that the property itself is private
so not accessible from your class. They only created a getter
to get the property but not a setter
. They could probably create a setter for this, but whenever the code for setting this variable changes they have to change it at the base of the class as well and vice versa. Best practice is to have a single access point for setting properties otherwise things might get really messy, here they choose for having transform.eulerAngles
as the access point for setting the euler angles x, y and z at once.