Question by
modernator24 · Mar 03, 2019 at 01:29 PM ·
unity 5uitext
Changing text occurs error: move initialization code to the Awake or Start function
I have GameManager class that has Log method:
using UnityEngine;
using UnityEngine.UI;
public class GameManager: MonoBehaviour {
public static GameManager Instance;
[SerializeField] private ScrollRect m_GUIConsoleContainer;
[SerializeField] private UnityEngine.UI.Text m_GUIConsole;
...
void Awake() {
Instance = this;
}
...
public void Log(string message) {
m_GUIConsole.text = m_GUIConsole.text + message + "\n";
}
}
GameManager class has static instance, so I can invoke Log method in anywhere like this:
GameManager.Instance.Log("Helloworld");
However it works only first time. After invoke Log again, Unity gives me this error message:
get_isActiveAndEnabled can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.UI.Text:set_text(String)
I assigned reference of m_GUIConsoleContainer and m_GUIConsole in inspector, but I don't get it what this means and why it happens. Just using Debug.Log() or print() works without any error.
How should I avoid this error message? Using Unity 2018.2.1f1.
Comment
Where and when do you call Game$$anonymous$$anager.Instance.Log("Helloworld");
? Who calls it? How?