Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在Unity中的第一个C#游戏(介绍playerprefs)_C#_Unity3d - Fatal编程技术网

我在Unity中的第一个C#游戏(介绍playerprefs)

我在Unity中的第一个C#游戏(介绍playerprefs),c#,unity3d,C#,Unity3d,在我的游戏中,有一个利润计数,它由我的货币脚本中的addMoney变量控制,例如profitcount=addMoney 当我添加关于addMoney变量的player pref时,它默认profitCount为0,而实际上它应该为1。这是我的第一场比赛,所以这很容易成为我误解或忽略的一件小事 金钱计数 public class moneyCount : MonoBehaviour { float timeTillAdd = 1; public int addMoney =

在我的游戏中,有一个利润计数,它由我的货币脚本中的addMoney变量控制,例如profitcount=addMoney

当我添加关于addMoney变量的player pref时,它默认profitCount为0,而实际上它应该为1。这是我的第一场比赛,所以这很容易成为我误解或忽略的一件小事

金钱计数

public class moneyCount : MonoBehaviour
{

    float timeTillAdd = 1;

    public int addMoney = 1;

    public int money;

    public Text txt;

    // Start is called before the first frame update
    void Start()
    {
        money = PlayerPrefs.GetInt("Money");
        addMoney = PlayerPrefs.GetInt("addmoney");
    }

    // Update is called once per frame
    void Update()
    {
        PlayerPrefs.SetInt("addmoney", addMoney);


        if (Time.time >= timeTillAdd)
        {
            money += addMoney;
            timeTillAdd++;

        }


        txt.text = money.ToString();

        PlayerPrefs.SetInt("Money", money);



    }


}
利润计算

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


public class profitCount : MonoBehaviour
{

    public int profitAmount;

    public GameObject moneyManagerObj;
    moneyCount mc;

    public Text txt;

    // Start is called before the first frame update
    void Start()


    {
        mc = moneyManagerObj.GetComponent<moneyCount>();
       // profitAmount = PlayerPrefs.GetInt("profitamount");
    }

    // Update is called once per frame
    void Update()
    {
        profitAmount = mc.addMoney;
        txt.text = profitAmount.ToString();

      //  PlayerPrefs.SetInt("profitamount", profitAmount);

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类利润账户:单一行为
{
公共int profitAmount;
公共游戏对象MoneyManagerRobj;
moneyCount mc;
公共文本文本;
//在第一帧更新之前调用Start
void Start()
{
mc=moneyManagerObj.GetComponent();
//profitAmount=PlayerPrefs.GetInt(“profitAmount”);
}
//每帧调用一次更新
无效更新()
{
profitAmount=mc.addMoney;
txt.text=profitAmount.ToString();
//PlayerPrefs.SetInt(“profitamount”,profitamount);
}
}
店长

public class ShopManager : MonoBehaviour
{

    public int item1_cost = 50;
    public int item1_upgrade = 5;
    public int item1_tier = 1;
    public int item2_cost = 50;
    public int item2_upgrade = 5;
    public GameObject moneyManagerObj;
    moneyCount mc;

    public Text txt;
    public Text item1;
    public Text item1_text;

    // Start is called before the first frame update
    void Start()
    {


        mc = moneyManagerObj.GetComponent<moneyCount>();
        item1_cost = PlayerPrefs.GetInt("Item1_cost");
        //item1_upgrade = PlayerPrefs.GetInt("Item1_upgrade");
        item1_tier = PlayerPrefs.GetInt("Item1_tier");


    }

    // Update is called once per frame
    void Update()
    {
        item1.text = item1_tier.ToString();

        PlayerPrefs.SetInt("Item1_cost", item1_cost);
        //  PlayerPrefs.SetInt("Item1_upgrade", item1_upgrade);
        PlayerPrefs.SetInt("Item1_tier", item1_tier);

        if (item1_tier > 0)
        {



            item1_text.text = ("Upgrade");





        }


    }




    public void on_click()


    {


        {

            if (mc.money >= item1_cost)
            {




                mc.money -= item1_cost;
                mc.addMoney += item1_upgrade;
                item1_tier += 1;
                item1.text = item1_tier.ToString();
                item1_cost += 50 * item1_tier;


            }



        }
    }














}
公共类商店经理:MonoBehavior
{
公共int项目1_成本=50;
公共int项目1_升级=5;
公共整数项1_tier=1;
公共int项目2_成本=50;
公共整数项2_升级=5;
公共游戏对象MoneyManagerRobj;
moneyCount mc;
公共文本文本;
公共文本项目1;
公共文本项目1_文本;
//在第一帧更新之前调用Start
void Start()
{
mc=moneyManagerObj.GetComponent();
item1_成本=PlayerPrefs.GetInt(“item1_成本”);
//item1_upgrade=PlayerPrefs.GetInt(“item1_upgrade”);
item1_tier=PlayerPrefs.GetInt(“item1_tier”);
}
//每帧调用一次更新
无效更新()
{
item1.text=item1_tier.ToString();
PlayerPrefs.SetInt(“项目1成本”,项目1成本);
//PlayerPrefs.SetInt(“项目1\u升级”,项目1\u升级);
PlayerPrefs.SetInt(“项目1\u层”,项目1\u层);
如果(项目1\u层>0)
{
item1_text.text=(“升级”);
}
}
单击时出现公共无效()
{
{
如果(mc.money>=项目1_成本)
{
mc.money-=项目1_成本;
mc.addMoney+=项目1\u升级;
项目1_tier+=1;
item1.text=item1_tier.ToString();
项目1_成本+=50*项目1_层级;
}
}
}
}

确定,因此您在初始化期间为
addMoney
分配了默认值,即
public int addMoney=1

但在开始时,您再次为其分配了一个尚未保存的值
addMoney=PlayerPrefs.GetInt(“addMoney”)

如果您想创建一条记录,请按如下方式操作
PlayerPrefs.SetInt(“addmoney”,addmoney)

两件事:
  • 不要使用
    PlayerPrefs
    存储保存的数据。这不是为了那个。它的意思是保存播放器的首选项,如音量、全屏模式或输入类型。
    PlayerPrefs
    使用的文件是纯文本,不能支持复杂的数据类型

  • 如果不存在保存数据,则读取的值为零(至少从PlayerPrefs读取)。您需要对此进行说明,但目前您没有。当您移动到另一种保存方法时,您将得到其他错误,如空指针或文件未找到。您必须确定某个存储是否存在,并且只有当它存在时,才应该从中读取


  • 尝试删除
    addMoney=PlayerPrefs.GetInt(“addMoney”)来自您的
    moneyCount
    脚本。然后运行它并告诉这里它是否工作是的,当我移除播放器pref时它工作,这就是我在没有它的情况下遇到的问题,它从1开始,然后像正常情况一样上升,但是当我添加player pref函数时,它从零开始,因此不可能在游戏中赚钱。你理解这个问题吗?@thirteen3054你应该把它表述出来,并将其作为一个答案添加;)删除player pref可以解决这个问题,但是当游戏关闭并重新打开时,该值将不会保存。在调用get之前,不需要设置首选项,存在允许您指定默认值的GetInt重载。这样就不需要在调用GetInt之前调用SetInt。@Eddge What不会使这个答案出错;)因为OPs错误是在存储正确的值之前用0覆盖本地起始值;)