簡體   English   中英

2018 Unity Void Update()不更新第二行嗎?

[英]2018 Unity Void Update () not updating second line?

因此,我正在制作一個Tamagotchi寵物游戲,並且在統計信息中,“ Void Update()”中的行似乎只會更新_Happiness中的統計信息第一行,而不是_hunger中的行。 有什么原因可能會發生這種情況? 相對於統計數據,我的原型中還包含另一個腳本。

public GameObject happinessText;
public GameObject hungerText;

public GameObject robot;
// Update is called once per frame
void Update()
{
    happinessText.GetComponent<Text>().text = "" + robot.GetComponent<Robot>().happiness;
    hungerText.GetComponent<Text>().text = "" + robot.GetComponent<Robot>().hunger;
    //happinessText.GetComponent<Text>().text = "" + robot.GetComponent<Robot>().happiness;
}

其他腳本:

public int _hunger;
public int _happiness;


void Start()
{ 
PlayerPrefs.SetString("then", "05/06/2016 11:20:12");
    updateStatus();
}



    void updateStatus()
{
   if (!PlayerPrefs.HasKey("_hunger"))
    {
        _hunger = 100;
        PlayerPrefs.SetInt("_hunger", _hunger);

    } else {
        _hunger = PlayerPrefs.GetInt("_hunger");
    }

    if (!PlayerPrefs.HasKey("_happiness"))
    {
        _happiness = 100;
        PlayerPrefs.SetInt("_happiness", _happiness);

    } else {

        _happiness = PlayerPrefs.GetInt("_happiness");

    }

    if (!PlayerPrefs.HasKey("then")) //{
        PlayerPrefs.SetString("then", getStringTime());

        TimeSpan ts = GetTimeSpan();

        _hunger -= (int)(ts.TotalHours * 2);
        if (_hunger < 0)
            _hunger = 0;

        _happiness -= (int)((100 - _hunger) * ts.TotalHours / 5);

        if (_happiness < 0)
            _happiness = 0;

    }

public int hunger
{
    [OnSerialized]
    get { return _hunger; }
    set { _hunger = value; }
}

public int happiness
{
    [OnSerialized]
    get { return _happiness; }
    set { _happiness = value; }
}

在其他腳本的Start方法中,似乎方法updateStatus僅被調用一次。

查看腳本,您似乎可能缺少Update()函數:

void Update()
{
    updateStatus();
}

我還將在更新_hunger_happiness的代碼中_hunger一些斷點。 或根據需要放置一些Debug.Log(...)調用。 重要的是要確認值是否定期更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM