BaN*_*Nya -1 c# unity-game-engine
首先,我很抱歉这段文字看起来很奇怪。这是我第一次使用stackoverflow
我开始学习Unity和C#。
今天,我了解了如何统一移动多维数据集,要回顾一下脚本,我认为我失败了。
我将脚本放在Hierarchy的cube1中,单击在C#处构建的解决方案并统一运行。而且没有用。
public class TRAIN : MonoBehaviour
{
// return cube1 to cube. cube1 is name of cube object in unity
GameObject cube = GameObject.Find("cube1");
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//move cube1 to z-axis at speed 1
cube.transform.position += new Vector3(0, 0, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
如何移动多维数据集1?
您不能GameObject.Find()直接在那儿打电话,您应该在控制台中遇到错误。
UnityException:不允许从MonoBehaviour构造函数(或实例字段初始化程序)中调用Find,请在Awake或Start中调用它
用Awake()或Start()代替。
public class TRAIN : MonoBehaviour
{
GameObject cube;
void Start()
{
cube = GameObject.Find("cube1");
}
Run Code Online (Sandbox Code Playgroud)