簡體   English   中英

Unity:值仍未更新-UPDATED

[英]Unity: Value still not updating - UPDATED

更新:答案中的代碼不起作用,但是我取得了一些進步,但是仍然有問題。 更新。

這是我的UpgradeUI腳本:

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

OnClick函數也可以與Debug.Log一起使用。 下面的代碼是Stats腳本,該腳本也更新了我的HealthUI。

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

[Serializable]
public class Stats
{
    public static Stats st;
    [SerializeField]
    private BarScript bar;
    public float maxVal;
    public float currentVal;
    public int playerDamage = 20;


    public float CurrentVal
    {
        get
        {
            return currentVal;
        }

        set
        {
            this.currentVal = Mathf.Clamp(value, 0, maxVal);
            bar.Value = currentVal;
        }
    }


    public float MaxVal
    {
        get
        {
            return maxVal;
        }

        set
        {
            this.maxVal = value;
            bar.MaxValue = value;
        }
    }
    public void Initialize()
    {

        this.MaxVal = maxVal;
        this.CurrentVal = currentVal;
    }
}

我確實進步了一點。 我放置了以下代碼: public void HpUp () { stats.maxVal += 20; stats.currentVal += 20; } public void DmgUp() { stats.playerDamage += 10; } public void HpUp () { stats.maxVal += 20; stats.currentVal += 20; } public void DmgUp() { stats.playerDamage += 10; }

進入我的PlayerManager並與我共享的Stats腳本連接,並且我的健康狀況確實會更新。 我刪除了UpgradeMenuUI。 現在的問題是我的傷害沒有更新。 嗯,實際上它會更新,但價值並沒有達到我需要的價值。 我加了

 public int playerDamage;

在我的統計腳本中。 在我的損害腳本中,該腳本計算了對玩家和敵人的損害,

  aiManager.Enemy1Damage(playerDamage); to
      aiManager.Enemy1Damage(stats.playerDamage);

我打電話從統計我playerDamage到我的這些傷害腳本: public Stats stats;

 public void Awake()
     {
         stats = new Stats();
     }

所以問題是,如果我從this: public int playerDamage;更改我的狀態腳本中的playerDamage this: public int playerDamage;

public int playerDamage = 20;

該值確實有效,當我擊中AI時,我的玩家傷害確實為20。 但是在檢查器中,我的傷害(來自Stats腳本的public int)為0,如果我按升級傷害按鈕,傷害會增加10(這是我在PlayerManager腳本中設置的值):

 public void DmgUp()
 {
     stats.playerDamage += 10;
 }

),但不會以任何方式影響值20。因此,它就像抽空氣一樣毫無用處。 我不知道如何使公共int playerDamage從OnClick更新。 如果只是將其設為playerDamage; 沒有數字,則不會更新,並且損害為0。

您將統計信息作為單例,但是正在按鈕上訪問它的新實例。 嘗試更改:

public void HpUpgrade()
{
    stats.maxVal = (int)(stats.Instance.maxVal += hpUp);

}
public void DmgUpgrade()
{
    dmg.playerDamage = (int)(stats.Instance.maxVal += dmgUp);
}

我不太清楚您是否已經確認您的OnClick事件正在觸發按鈕。 如果不是,請確保場景中有EventSystem游戲對象。

暫無
暫無

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

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