Aiman Khan
Aiman Khan

Reputation: 21

Why can't I update value of Unity Text in my function?

I am trying to display error messages according to what type of error occurs. For that, I have a public variable "errorMessage" which is attached to UI Text in the inspector. I am able to change the text value in start function but can't seem to change that value in GetErrorMessage function. Following is the code:

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

Upvotes: 0

Views: 353

Answers (1)

Lyrca
Lyrca

Reputation: 613

Your function runs until the end, unless you're getting a NullReferenceException (which is the only possible exception you can get from that method).

What I believe is happening is that in your Console panel , you have the "Collapse" option ticked, so the message stacks instead.

enter image description here

If you want to make sure it runs, in your last line of GetErrorMessage() print "Error function exits" instead.

Upvotes: 1

Related Questions