- Home /
LocalEulerAngles with random rotation doesn't work
Hi, I have a method in my script which is supposed to rotate by a random number when instantiating. The rotation script goes like this:
newStructure.transform.localEulerAngles.y = (Random.Range (0,360));
And the error I get goes like this:
Assets/GridBehaviour.cs(55,64): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localEulerAngles'. Consider storing the value in a temporary variable
Do you have any idea how I would solve this? Some Google results said that you have to have all Vector3 proprieties, but since this doesn't use Vector3 I think those are just to fool me.
Thanks!
Answer by zombience · Oct 23, 2013 at 07:22 PM
Unfortunately you cannot modify individual x, y, or z values of transform.position OR transform.localEulerAngles;
You must create a new Vector3, change one value, and then reassign it to the transform property.
Like so:
Vector3 temp = newStructure.transform.localEulerAngles;
temp.y = Random.Range(0, 360);
newStructure.transform.localEulerAngles = temp;
Your answer

Follow this Question
Related Questions
Why my rotation gets different values? 1 Answer
is there any way to get a sane number on an objects current y rotation? 2 Answers
Rotating GameObject with transform.Rotate issue! 1 Answer
Object spinning around it's own axis whilst travelling in a straight line 1 Answer
Random z rotation instantiate 2 Answers