在Unity3d的游戏中,我处理保存/加载游戏,我希望显示player.transform.position
,但它始终显示0, 0, 0
。无法理解为什么
public GameObject player;
private Transform playerTransform;
//..
playerTransform = player.GetComponent<Transform>();
保存数据
PlayerPrefs.SetFloat("x" + currentActiveSlot, playerTransform.position.x);
PlayerPrefs.SetFloat("y" + currentActiveSlot, playerTransform.position.y);
PlayerPrefs.SetFloat("z" + currentActiveSlot, playerTransform.position.z);
PlayerPrefs.Save();
加载数据
Debug.Log("transform before: " + transform.position); // all time 0,0,0
playerTransform.position = new Vector3(PlayerPrefs.GetFloat("x" + currentActiveSlot),
PlayerPrefs.GetFloat("y" + currentActiveSlot),
PlayerPrefs.GetFloat("z" + currentActiveSlot));
Debug.Log("transform after: " + transform.position); // all time 0,0,0
答案 0 :(得分:3)
您正在显示未在任何地方设置的transform.position。
加载数据
Debug.Log("transform before: " + transform.position); // all time 0,0,0
transform.position = new Vector3(PlayerPrefs.GetFloat("x" + currentActiveSlot),
PlayerPrefs.GetFloat("y" + currentActiveSlot),
PlayerPrefs.GetFloat("z" + currentActiveSlot));
Debug.Log("transform after: " + transform.position); // all time 0,0,0
或
Debug.Log("transform before: " + playerTransform.position); // all time 0,0,0
playerTransform.position = new Vector3(PlayerPrefs.GetFloat("x" + currentActiveSlot),
PlayerPrefs.GetFloat("y" + currentActiveSlot),
PlayerPrefs.GetFloat("z" + currentActiveSlot));
Debug.Log("transform after: " + playerTransform.position); // all time 0,0,0