- Home /
NullReferenceException in UI TextElement
So I've been following this basic tutorial about Unity when I ran into issues with trying to run a create a score variable that displays in the UI.
using UnityEngine.UIElements;
using UnityEngine;
public class Score : MonoBehaviour
{
public Transform player;
public TextElement score;
void Update()
{
score.text = player.position.z.ToString("0");
}
}
This is the error I got: NullReferenceException: Object reference not set to an instance of an object Score.Update () (at Assets/Scripts/Score.cs:12)
Also I cannot put an object in the inspector even though I declared TextElement as public.
Here is your answer:
so you said it yourself - field is null. Don't be surprised your computer noticed that too at line 12.I cannot put an object in the inspector even though I declared TextElement as public
I understand that I need to instantiate an object, but I'm not sure how to do that in Unity. I should preface that I've only been learning Unity for a couple hours.
Answer by andrew-lukasik · Oct 29, 2021 at 08:25 AM
UiDocumentController.cs
using UnityEngine;
using UnityEngine.UIElements;
public class UiDocumentController : MonoBehaviour
{
[SerializeField] UIDocument _uiDocument = null;
Label _timer;
void OnEnable ()
{
OnBind( _uiDocument.rootVisualElement );
}
void Update ()
{
_timer.text = $"Time.time: {Time.time:0.00}";
}
void OnBind ( VisualElement root )
{
_timer = root.Q<Label>("timer");
}
}
Add UIDocument component, make sure it's
Source Asset
field is filled with anUXML
file you builtAdd
UiDocumentController
component (src above) and fill it'sUiDocument
fieldEdit
UiDocumentController
so it does what you need and thatQ<Label>( ELEMENT NAME )
exist in the uxml file
This seems like a very thorough answer to my question, but I feel like there should be a simpler way to do something like this, even if it's not good practice. I'm just trying to change the text in the UI to update with the change in the position of the player. It seems in the 2018 version of Unity it was as simple as I typed. I really appreciate the time and effort you put into your answer regardless!
Your code sample contains UnityEngine.UIElements
so I presumed this is what you were after here. But if that is not the case you need to know that Unity has 3 separate UI systems at this point:
IMGUI (old, simple but inefficient and nearly obsolete)
UGUI (intuitive 3D UI system, easy to start but gets hard to work with as project grows)
UI Elements / UI Toolkit - this project was renamed not long ago, so both names refer to the same thing (very web-like (similar to html+css but with C# instead of JS), good workflow and decent performance, still in development, best option to learn for the long-term use imho)
So, if you just want to display some random text on screen you can start with IMGUI, as it's super simple for beginners and will introduce you to the basic concepts:
using UnityEngine;
public class GUITest : MonoBehaviour
{
public Transform player;
void OnGUI ()
{
GUI.Label (new Rect (25, 25, 100, 30), $"Score: {player.position.z:0}");
}
}
Thank you so much for that information!
Is there a way for me to change the Text in the Canvas with a script? (Right now it's 0)
Your answer
Follow this Question
Related Questions
Graphic problem with UI Text 2 Answers
New UI: change text 2 Answers
UGUI Find tapped character in textfield? 1 Answer