2016-11-22 83 views
0

嘿所以我嘗試了unitys Playerprefs方法,不知怎麼,它不會保存硬幣,當我關閉並退出遊戲,它不會將它保存...PlayerPrefs不工作

public Text CoinsText; 
public int Coins; 
public int clicks; 


void Start() 
{ 
    PlayerPrefs.GetInt("Coins", Coins); 
} 
void Update() 
{ 
    CoinsText.text = "Memes: " + Coins; 
    if (Input.GetMouseButtonDown(0)) 
    { 
     PlayerPrefs.SetInt("Coins", Coins); 
     Coins += clicks; 

    } 
} 

}

+0

你不應該指派PlayerPrefs.GetInt(「Coins」,Coins);硬幣?我從來沒有使用過玩家首選項,但我猜它只返回一個值? – Alox

+0

獲取int將加載它 –

回答

1

你」從來沒有分配給你的硬幣。

在你開始()試試這個:

Coins = PlayerPrefs.GetInt("Coins"); 

注意,第二個值僅在情況下,就不會被保存的值。

public static int GetInt(string key, int defaultValue = 0); 

另外,不要忘了關閉程序之前保存的所有值:

PlayerPrefs.Save(); 
+0

謝謝!!!!作品 –

+0

隨時接受爲答案。 – Alox

+0

@FatihTkale就像FCin提到的那樣,你的PlayerPrefs.Save()是哪裏?這只是一個代碼片斷,你忘了添加它? – Alox

0

,首先你應該得到詮釋這樣Coins = PlayerPrefs.GetInt("Coins");

再經過你,你有SetIntPlayerPrefs.Save();