【问题标题】:Why can't I update value of Unity Text in my function?为什么我不能在我的函数中更新 Unity Text 的值?
【发布时间】:2020-03-02 22:33:19
【问题描述】:

我正在尝试根据发生的错误类型显示错误消息。为此,我有一个公共变量“errorMessage”,它附加到检查器中的 UI 文本。我可以在 start 函数中更改文本值,但似乎无法在 GetErrorMessage 函数中更改该值。以下是代码:

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


public class LoginController : MonoBehaviour
{
    public InputField email, password; 
    public Screen nextScreen;
    public Text errorMessage;

    void Start()
    {
        password.inputType = InputField.InputType.Password;
        errorMessage.text = "This is error"; //this works
    }


    public void Login()
    {
        //login code which calls GetErrorMessage(error)
    }
    void GetErrorMessage(string error)
    {
        print("error function called");
        print(error);
        print(errorMessage.text); //works till here
        errorMessage.text = error; //doesn't work from here. Seems like it stops functioning.
        print(error); //and this never gets printed
    }
}

Inspector: Error Message is attached to my UI text

【问题讨论】:

  • 并且没有错误信息???你有没有机会在一个线程中这样做?
  • 在更新时应用错误消息,您可以在 GetErrorMessage 方法上使用 bool 来标记更新,并在 Update 中使用该 bool 的条件来应用更改。

标签: unity3d unity-ui


【解决方案1】:

除非您收到 NullReferenceException(这是您可以从该方法获得的唯一可能异常),否则您的函数会一直运行到最后。

我相信正在发生的事情是,在您的控制台面板中,您勾选了“折叠”选项,因此消息会堆叠起来。

如果您想确保它运行,请在 GetErrorMessage() 的最后一行打印“错误函数退出”。

【讨论】:

  • 我检查了没有 NullReferenceException。但是我的问题现在已经解决了,感谢@joreldraw。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多