Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by L-Man20008 · Oct 29, 2021 at 08:33 AM · uitextuitoolkit

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Oct 29, 2021 at 06:47 AM 0
Share

Here is your answer:

I cannot put an object in the inspector even though I declared TextElement as public

so you said it yourself - field is null. Don't be surprised your computer noticed that too at line 12.
avatar image L-Man20008 andrew-lukasik · Oct 29, 2021 at 02:38 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 an UXML file you built

  • Add UiDocumentController component (src above) and fill it's UiDocument field

  • Edit UiDocumentController so it does what you need and that Q<Label>( ELEMENT NAME ) exist in the uxml file



Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image L-Man20008 · Oct 29, 2021 at 02:51 PM 0
Share

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!

avatar image andrew-lukasik L-Man20008 · Oct 29, 2021 at 05:49 PM 0
Share

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:

  1. IMGUI (old, simple but inefficient and nearly obsolete)

  2. UGUI (intuitive 3D UI system, easy to start but gets hard to work with as project grows)

  3. 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}");
     }
 }
avatar image L-Man20008 andrew-lukasik · Oct 29, 2021 at 06:45 PM 0
Share

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)

alt text

unityhelp.png (96.6 kB)
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

227 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Graphic problem with UI Text 2 Answers

New UI: change text 2 Answers

UGUI Find tapped character in textfield? 1 Answer

How to attach a canvas to a game object properly? 1 Answer

Item name UI/GUI text over item/enemy in 3d 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges