如何在Unity中显示player.transform.position?

时间:2016-12-26 08:58:47

标签: c# unity3d

在Unity3d的游戏中,我处理保存/加载游戏,我希望显示player.transform.position,但它始终显示0, 0, 0。无法理解为什么

enter image description here

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

1 个答案:

答案 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