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.


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;