SquidCoin.cs添加[PlayerPrefs]后,我的货币脚本不起作用

时间:2018-08-29 23:32:54

标签: c# unity3d

我有一个游戏的货币脚本,该脚本没有显示用户拥有多少钱。

在添加PlayerPrefs类之前,它一直在工作。 但是添加之后,它将不会显示任何内容。

仅供参考,PlayerPrefs类允许我在Unity中的多个场景中显示数字。

我在下面发布了我的代码

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   using UnityEngine.UI;

public class squidCoin : MonoBehaviour {


public Text coinDisplay;
public int addMoneyAmmount;
public int squidCoins;
public float saveInterval;
public float paycheckTime;

// Use this for initialization
void Start () {
squidCoins = 50;
PlayerPrefs.SetInt("SquidCoins", squidCoins);
 StartCoroutine(moneyADD());
 StartCoroutine("SaveMoney");


  }

public void squidCoinPayCheck(int squidCoinsToAdd){
    squidCoins += squidCoinsToAdd;

  }

  public void Awake(){
  PlayerPrefs.GetInt("SquidCoinsSaves");
  }

public void minusSquidCoin(int squidCoinsToSubtract){
if(squidCoins - squidCoinsToSubtract < 0){
        Debug.Log ("Oops Hes Broke");
        squidCoins += addMoneyAmmount;
    }
    else{
    squidCoins -= squidCoinsToSubtract;
    }

}


  IEnumerator moneyADD(){
     yield return new WaitForSeconds(paycheckTime);
     squidCoins += addMoneyAmmount;
 }

 IEnumerator SaveMoney (){
     while (true)
     {
             yield return new WaitForSeconds(saveInterval);
             PlayerPrefs.SetInt("SquidCoinSaves", squidCoins);

     }
 }


// Update is called once per frame
void Update () {
 coinDisplay.text = "You Have: " + PlayerPrefs.GetInt("SquidCoinsSaves");
}
}

1 个答案:

答案 0 :(得分:0)

我查看了所有代码,似乎有很多错误的细节。我刚刚修复它,并在错误的部分前面加了注释,指出了您的错误

using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   using UnityEngine.UI;

public class squidCoin : MonoBehaviour {


public Text coinDisplay;
public int addMoneyAmmount;
public int squidCoins;
public float saveInterval;
public float paycheckTime;

// Use this for initialization
void Start () {
squidCoins = 50;
PlayerPrefs.SetInt("SquidCoinsSaves", squidCoins); //your string name was wrong
 StartCoroutine(moneyADD());
 StartCoroutine("SaveMoney");


  }

public void squidCoinPayCheck(int squidCoinsToAdd){
    squidCoins += squidCoinsToAdd;

  }

  public void Awake(){
  //PlayerPrefs.GetInt("SquidCoinsSaves"); //you can not get it like this, first you must equal this ti intiger type varible
  }

public void minusSquidCoin(int squidCoinsToSubtract){
if(squidCoins - squidCoinsToSubtract < 0){
        Debug.Log ("Oops Hes Broke");
        squidCoins += addMoneyAmmount;
    }
    else{
    squidCoins -= squidCoinsToSubtract;
    }

}


  IEnumerator moneyADD(){
     yield return new WaitForSeconds(paycheckTime);
     squidCoins += addMoneyAmmount;
 }

 IEnumerator SaveMoney (){
     while (true)
     {
             yield return new WaitForSeconds(saveInterval);
             PlayerPrefs.SetInt("SquidCoinsSaves", squidCoins); //string name was wrong

     }
 }


// Update is called once per frame
void Update () {
 coinDisplay.text = "You Have: " + PlayerPrefs.GetInt("SquidCoinsSaves");
}
}