- Home /
Cannot modify a value type return value of `UnityEngine.Transform.localRotation'. Consider storing the value in a temporary variable. Help
My problem is this line transform.localRotation.eulerAngles = new Vector3 (0.0f, curY, 0.0f);
And its not working. Its from java source and i want it to be converted to C#.
Answer by rutter · Jun 04, 2014 at 04:18 PM
When you request localRotation
, C# gives you a copy of it. Changes made to the copy are ignored by the original. That's because Quaternion
is a struct type.
You can fix this by declaring a temporary variable:
Quaternion rot = transform.localRotation;
rot.eulerAngles = new Vector3 (0.0f, curY, 0.0f);
transform.localRotation = rot;
Or, you can just assign the value directly:
transform.localRotation = Quaternion.Euler(0.0f, curY, 0.0f);
You'll see this same behavior with vectors (such as transform.position
).
Hi, this is related so I hope you dont $$anonymous$$d. Im trying to read csv values taken from an accelerometer module and use them to animate a 3DCube. I have this so far but the cube doesnt rotate:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
public class CSVFileReader : $$anonymous$$onoBehaviour
{
public GameObject cubeTest;
public TextAsset csvFile;
public string thing = "krakozia";
public float retraso = 0.1f;
bool canRotate = true;
Vector3 gyroRotation;
Transform cubeTransform;
void Start () {
cubeTransform = cubeTest.GetComponent<Transform>();
}
void Update ()
{
if (canRotate) {
StartCoroutine(readCSV());
}
}
YieldInstruction wait5sec = new WaitForSeconds(0.5f);
IEnumerator readCSV()
{
canRotate = false;
string[] records = csvFile.text.Split('\n');
for(int i = 1; i < records.Length; i++)
{
string[] fields = records[i].Split(',');
Debug.Log ("what" + thing);
float x = float.Parse(fields[0]);
float y = float.Parse(fields[1]);
float z = float.Parse(fields[2]);
Quaternion rot = cubeTransform.rotation;
rot.eulerAngles = new Vector3(x,y,z);
cubeTransform.rotation = rot;
}
yield return wait5sec;
canRotate = true;
}
Your answer
Follow this Question
Related Questions
Aiming in code only. 1 Answer
aim script error 1 Answer
c# modify only one axis of a quaternion 2 Answers
Rotating wrong 0 Answers
How do I add up all the rotations of a Tranform list ? 1 Answer