所以我創建一個保存和加載腳本,這是它的內容:團結告訴我通過一個對象的實例引用時,Visual Studio告訴我不要
using UnityEngine;
using System.Collections;
public class SaveLoad : MonoBehaviour {
public Player player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
public void Save() {
PlayerPrefs.SetFloat ("X", transform.position.x);
PlayerPrefs.SetFloat ("Y", transform.position.y);
PlayerPrefs.SetFloat ("Z", transform.position.z);
PlayerPrefs.SetFloat("Health", Player.Health.CurrentVal);
PlayerPrefs.SetFloat ("Exp", Player.Exp.CurrentVal);
PlayerPrefs.SetFloat("Oxygen", Player.Oxygen.CurrentVal);
PlayerPrefs.SetFloat("PlayerLevel", Player.PlayerLevel.CurrentVal);
Debug.Log ("Saved");
}
public void Load() {
float x = PlayerPrefs.GetFloat ("X");
float y = PlayerPrefs.GetFloat ("Y");
float z = PlayerPrefs.GetFloat ("Z");
Player.Health.CurrentVal = PlayerPrefs.GetFloat("Health");
Player.Exp.CurrentVal = PlayerPrefs.GetFloat("Exp");
Player.Oxygen.CurrentVal = PlayerPrefs.GetFloat("Oxygen");
Player.PlayerLevel.CurrentVal = PlayerPrefs.GetFloat("PlayerLevel");
transform.position = new Vector3 (x, y, z);
Debug.Log ("Loaded");
}
}
的第一個版本,沒有「公共播放器播放器「行,返回一個NullReferenceException並表示通過Unity中的一個對象實例引用。現在,當我嘗試這樣做(而不是(「健康」,Player.Health.CurrentVal)我使用(「健康」,player.Health.CurrentVal),沒有資本在「播放器」,引用我在同樣的腳本),Visual告訴我「Player.Health不能通過一個對象的實例來訪問;改爲使用類型名稱來對其進行限定」。正如你所看到的,我有點陷入循環。我是否搞砸了將玩家作爲一個實例?這也有點混亂,因爲玩家既是遊戲對象的名字,也是腳本的。 播放器腳本:
[SerializeField] public Stat health;
public static Stat Health { get; set; }
[SerializeField] private Stat exp;
public static Stat Exp { get; set; }
[SerializeField] private Stat oxygen;
public static Stat Oxygen { get; set; }
[SerializeField] private Stat playerLevel;
public static Stat PlayerLevel { get; set; }
[SerializeField] private Text playerLevelText;
而這裏的統計自定義變量類:
[SerializeField] private BarPrototype bar;
[SerializeField] private float maxVal;
[SerializeField] private float currentVal;
public float CurrentVal
{
get
{
return currentVal;
}
set
{
this.currentVal = Mathf.Clamp (value, 0, MaxVal);
if(bar != null)
bar.Value = this.currentVal;
}
}
SerializeField使用只是爲了檢查訪問這些值。
你能從'Player'類發佈相關部分嗎? –
當然,看到第一篇文章@ botond.botos – Nech