在我的代码的Start()
函数中,我有代码将两个立方体的变换保存到变换数组中(在开始时,一切都保存得很好)。但是,当我在更新函数中设置保存的变换时,两个位置transformArray [0]和transformArray [1]具有相同的值!为什么会这样?我认为Start函数只运行一次?
每次按X(我的键事件)时,似乎都会调用start函数,为什么会发生这种情况?我没告诉它在我的代码中运行Start()函数。
Vector3 BoxPos;
public GameObject box1;
public GameObject box2;
Transform targetTransform;
public string curName;
public Transform[] transformArray;
void Start(){
curName = gameObject.name;
transformArray = new Transform[2];
transformArray[0] = box1.GetComponent<Transform>();
transformArray[1] = box2.GetComponent<Transform>();
Debug.Log ("@Box1:" + transformArray[0].position); // VALUE IS PROPER
Debug.Log ("@Box2:" + transformArray[1].position); // VALUE IS PROPER
}
void Update(){
if(Input.GetKeyDown(KeyCode.X)) {
if(curName == "box1"){
Debug.Log("&Box1:" + transformArray[1].position); //value=transformArray[1].position!
targetTransform = transformArray[1];
}else if(curName == "box2"){
Debug.Log("&Box2:" + transformArray[0].position);//value=transformArray[1].position!
targetTransform = transformArray[0];
}
BoxPos = targetTransform.position;
transform.position = BoxPos;
}
}