Pet*_*ter 1 c# unity-game-engine
抱歉,我已经使用 C# 多年了,我知道这是一个简单的问题。但是如何引用我的脚本所附加的游戏对象呢?这就是我具体想做的事情:
if (Component.gameObject.position.y < -29)
{
Component.gameObject.position.y = 2;
Component.gameObject.position.x = 0;
Component.gameObject.position.z = 0;
}
Run Code Online (Sandbox Code Playgroud)
您不会像static通过类访问字段一样访问它Component!
你的类已经是aComponent并且可以访问它自己的字段。请参阅提供的 API 示例!
这简直就是
this.gameObject
Run Code Online (Sandbox Code Playgroud)
大多数情况下,其中this可以省略。
无论如何 aGameObject没有position..只有Transform附加到它的组件。
幸运的是,已经Component有了快捷方式属性transform,它基本上等于gameObject.transform所以你想要的是
if(transform.position.y < 29)
{
transform.position = new Vector3(0,2,0);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3662 次 |
| 最近记录: |