【问题标题】:UI Text not changing after onClick function在 onClick 功能后 UI 文本没有改变
【发布时间】:2016-03-24 13:22:03
【问题描述】:

我在 CANVAS 1 中有一个名为 Total Points Text 的 UI 文本,在 CANVAS 2 中有一个图像。 一个按钮组件附加到图像上,因此当它被触摸时,图像会被破坏并应该更改 Total Points Text 中的文本。

问题是,调用 onClick of Image 的函数被调用,但 Total Points Text 中的 Text 没有改变。

奇怪的是,当我移动将文本更改为图像的 Start() 的行时,文本确实发生了变化...... 另一个奇怪的事情是,我确实用我的目标脚本(附加到我的角色的那个)更改了总点数文本几次,它工作正常。也许问题来自按钮-onClick?

这是我的代码:

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

public class twohundredPTSScript : MonoBehaviour
{
    public Image TWOHundredpointsImage;
    public Text TotalPointsText;


  
  
    public void Purchase200()
    {
         int totalpoints = ballscript.TotalPoints;

        if(totalpoints >= 200)
        {
            Destroy(TWOHundredpointsImage); // CALLED
            totalpoints = totalpoints - 200; // CALLED
            Debug.Log(totalpoints); // CALLED
            Debug.Log("Item bought !"); // CALLED

            TotalPointsText.text = "Total points: " + totalpoints.ToString(); // THIS ISN'T CALLED OR ISN'T WORKING.
        }

        else
        {
            Debug.Log("Not enough points !");
        }

        ballscript.TotalPoints = totalpoints;
    }
}


【问题讨论】:

    标签: c# user-interface button text unity3d


    【解决方案1】:

    我已经解决了这个问题,文本也以相同的值以每个帧速率 (Update()) 更新。所以我对上面脚本中的值所做的更改只发生在 1 帧,我没有注意到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多