shenhuan111 2014-12-20 13:28 采纳率: 64.3%
浏览 6200

unity c#中为什么不能直接更改transform.position.x的值?

The reason you can't directly alter the position vector is because it's actually a copy, not the actual position stored on the transform.

transform.position <--- returns a copy of the position
transform.position.x <--- the "x" value of the copy
transform.position.x = 7.0f <--- sets the "x" value on the copy

C# throws an error because you're setting the "x" value on a copy, which is then destroyed. It's a pointless line that if the compiler didn't pick up on it, could cause a tonne of hair pulling.

UnityScript gets around this when it compiles by converting your code and firing the C# setter:
transform.position.x = 7.0f
when compiled in UnityScript, translates (more or less) to:
var tempVector : Vector3 = transform.position;
tempVector.x = 7.0f;
transform.position = tempVector;

You can infer that because if you replicate this with a custom C# class, you can track as the getters/setters are invoked.


  • 写回答




      • ¥15 C#调用dll形参问题
      • ¥15 Unity 怎么创建树形菜单
      • ¥15 使用DWY100k数据集对UEA进行测试,出现报错:IndexError: index 125000 is out of bounds for axis 0 with size 95500
      • ¥15 前端vue实现根据图片url生成pdf文件
      • ¥15 RfidReader资源Q个
      • ¥20 user-agent是否是唯一的,有没有可能相同
      • ¥15 关于#开会#的问题,如何解决?(语言-c++)
      • ¥15 关于#二十四点问题#的问题
      • ¥15 运行kitex的demon出错(求大家解决)
      • ¥15 开发一个类似 typora 这样的软件需要多少钱?